【IoC部分】
对于Properties属性的设置
<bean
id="mappings"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfi
</bean>
内部Bean,用于只注入一次的Bean
<bean id="outer" class="...">
</bean>
使用p-namespace简化Bean的属性定义
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
</beans>
depends-on属性
depends-on可以显示的声明bean之间的依赖关系,还可以决定bean初始化与销毁的顺序
PropertyPlaceholderConfigurer,如果bean的一些属性不想写在XML配置文件中,可以使用此类写到Properties文件中,方便用户修改。
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfi
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
</bean>
下面是Properties文件的内容
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root
在Spring 2.5中利用context命名空间,也可以简写如下
<context:property-placeholder
location="classpath:配置文件1,配置文件2"/>
MessageSources,用来加载资源文件,可实现I18N
<beans>
</beans>
在web.xml中加载ApplicationContext,可以使用两种方式:Servlet或ServletListener,后者要求服务器实现Servlet
2.4规范,推荐使用。
<context-param>
</context-param>
<listener>
</listener>
其中参数contextConfigLocation的值支持ANT风格的通配符,比如/WEB-INF*Context.xml,表示WEB-INF及其子目录下所有以Context.xml结尾的文件
使用Annotation实现IoC
首先在配置文件中显式声明需要使用Annotation
<?xml version="1.0"
encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
</beans>
在setter方法前可以使用@Autowired表示自动装配、使用@Required表示属性需要被注入。@Autowired也可以用在非setter方法前、甚至用在构造函数或者属性前。
如果希望使用by-name注入,可以使用符合JSR-250的@Resource(name="xxxx")
@Repository, @Service, @Controller分别对应DAO,Service,控制器。可以用@Service(xxx)的方式给Bean起名字
自动检测组件<context:component-scan base-package="org.example"/>,可以用@scope控制实例数量
插入表情