spring 高级装配 第三章小结
本章主要讲解spring-ioc的高级功能。
profile
- 作用
- 为Bean添加profile配置,相当于为bean添加了环境标签。
在项目启动的过程中,根据主配置中的环境设置,仅初始化相同环境下的bean。
- 场景
- 当项目打包后,将遇到以下几种环境(开发、测试、生产等等),系统在不同环境中运行需要连接不同数据库。此时可以使用profile实现切换。
- 语法
- 主配置(普通项目)
- 主配置(web项目)
在servlet配置中设置spring.profiles.default属性,指定默认使用的环境标签。
<!-- springmvc核心配置 -->
<servlet>
<servlet-name>springDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-application.xml</param-value>
</init-param>
<init-param>
<param-name>spring.profiles.default</param-name>
<param-value>dev</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
- bean配置(注解方式)
在类上添加@Profile("test")注解,表示该类的环境标签为test。
@Component("provider")
@Profile("test")
public class TestProvider extends IProvider{
@PostConstruct
public void init() {
System.out.println("test init");
}
@PreDestroy
public void delete() {
System.out.println("test destroy");
}
}
- bean配置(xml方式-为整个文件设置环境标签)
- 在最外层beans标签中设置profile属性。
-
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring扫描配置 @author zbn @create 2012-4-1 1:11:17 -->
<beans
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
profile="dev">
</beans>
- bean配置(xml方式-为文件中部分beans设置环境标签)
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring扫描配置 @author zbn @create 2012-4-1 1:11:17 -->
<beans
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans">
<beans profile="dev">
</beans>
<beans profile="test">
</beans>
</beans>
- 实例
-
条件化的Bean
在项目运行期间,根据不同的条件控制是否初始化。
使用前提:spring4(含4)以上,只能与@Bean配合使用。
@Conditional
处理自动装配的歧义性
当依赖目标大于1的情况下,spring会报错,解决方法如下。
1. 利用@Primary注解设置优先属性,依赖时仅考虑优先对象,过滤掉非优先对象。(如果优先对象设置了多个,也会失败。)
2. 利用@Qualifier(“自定义标签”)注解 为重复的对象设置唯一标签,依赖时利用@Qualifier(“自定一标签”)进行匹配。(如果标签重复了,也会失败)
3. 可以为重复的对象设置多个自定义标签,依赖时通过自定义标签进行多重匹配。
bean的作用域
切记spring初始化的bean,在为设置的情况下,均为单例模式。
针对不用应用场景,应该选取不同作用于的bean。spring提供了以下四种作用域。
1. 单例模式
2. 原型模式(普通)
3. 会话模式(session)
4. 请求模式(request)
设置方法
注解方式
@Controller
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class A {
}
@Service
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class B {
}
@Repository
@Scope(
value=WebApplicationContext.SCOPE_SESSION,
proxyMode=ScopedProxyMode.INTERFACES
)
//proxyMode配置是因为在项目启动过程中,还没有session请求。无法初始化当前对象。采用这种方式实现后加载效果。
public class C {
}
@Component
@Repository
@Scope(
value=WebApplicationContext.SCOPE_REQUEST,
proxyMode=ScopedProxyMode.INTERFACES
)
public class D {
}
xml方式
<bean id="cc" class="chujie.demo.spring.ioc.a.C" scope="singleton" />
<bean id="c" class="chujie.demo.spring.ioc.a.C" scope="prototype"/>
<bean id="ccc" class="chujie.demo.spring.ioc.a.C" scope="session" >
<aop:scoped-proxy proxy-target-class="false"/>
</bean>
<bean id="cccc" class="chujie.demo.spring.ioc.a.C" scope="request" >
<aop:scoped-proxy proxy-target-class="false"/>
</bean>
bean注入的四种方式区别
@Service
针对服务层的注解。
@Controller
针对控制层的注解。
@Repository
针对DAO层的注解。
@Component
针对其他层的注解。
额外
1.@PostConstruct
写在方法上,指定方法创建时运行。
2.@PreDestroy
写在方法上,指定方法销毁时运行。
3.一个类上可以出现多个注解,没有影响。
bean依赖的两种方式区别
@Autowired
1.此注解属于spring。
2.默认按类型依赖,根据对象类型从spring容器中获取依赖对象。
3.设置按名称依赖,通过@Qualifier(name)注解实现按名称依赖,根据name值从spring容器中查找对象依赖。
4.默认情况下必须要求依赖对象存在,除非设置required=false。否则会启动报错。
@Resource
此注解属于J2EE.
默认按名称依赖,从spring容器中查找同名称对象依赖。
当找不到名称时,按类型查找依赖。
可以通过name属性指定依赖名称,指定name后将不会再按类型来查找。
未指定的情况下:
写在字段上:则按字段名查询。
写在set方法上:则按属性名查询。
相关点
1.一个属相上可以出现多个注解,没有影响。
运行时值注入
加载配置文件
在启动过程中通过加载配置文件的方式,实现运行时值注入效果。配置文件类型很多,此处介绍spring 的 Environment方式。
在类上通过@PropertySource(“路径”)关联properties文件,在类属性中添加Environment属性,spring将自动解析配置文件,并将解析对象赋值到env属性中。
@Component
@PropertySource("classpath:a.properties")
public class BA {
@Autowired
public Environment env;
}
# 原始图片存储路径
originalPhotoPath=G:\\image\\original
# 水印图片存储路径
watermarkPhotoPath=G:\\image
# 略缩图存储路径
thumbnailPhotoPath=G:\\image\\resize
属性占位符
作用
实现spring动态注入效果。将动态参数写在配置文件中,项目启动时根据占位符自动加载动态值。
name=张三
通过配置加载配置文件。
使用${…}占位符引用参数。
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring扫描配置 @author zbn @create 2012-4-1 1:11:17 -->
<beans
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans">
<c:property-placeholder location="classpath:c.properties" />
<bean id="c1" class="chujie.demo.spring.ioc.c.C1">
<property name="name" value="${name}"></property>
</bean>
</beans>