1.1 基于注解的IOC配置 既注解配置和xml配置要实现的功能都是一样的,都是要降低程序间的耦合.只是配置的形式不一样. 1.2 环境搭建 1.2.1 第一步:拷贝必备的jar包 需要多拷贝一个spring-aop-4.2.4.RELEASE.jar 1.2.2 创建xml文件,导入约束 <?xml version="1.0" encoding="UTF-8"?> <!-- 导入schema 约束的位置在: ..\spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html 文件中。 注意:要导入schema约束 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http:
http:
http:
</beans> 1.2.3 使用@Component注解配置管理的资源
ICustomerService {
saveCustomer(); }
@Component(value="customerService") @Scope(value="singleton")
CustomerServiceImpl
ICustomerService {
@Resource(name="customerDao2")
ICustomerDao customerDao =
; @Value("com.mysql.jdbc.Driver")
String driver; @Override
saveCustomer() { System.out.println(driver); customerDao.saveCustomer(); } } 持久层代码:
ICustomerDao {
saveCustomer(); }
@Repository("customerDao1")
CustomerDaoImpl
ICustomerDao { @Override
saveCustomer() { System.out.println("保存了客户111111111111111111"); } }
@Repository("customerDao2")
CustomerDaoImpl2
ICustomerDao { @Override
saveCustomer() { System.out.println("保存了客户2222222222222222222"); } } 测试类代码:
Client {
main(String[] args) {
ApplicationContext ac =
ClassPathXmlApplicationContext("bean.xml");
ICustomerService cs = (ICustomerService) ac.getBean("customerService"); cs.saveCustomer(); } } 配置文件: <?xml version="1.0" encoding="UTF-8"?> <!-- 我们导入约束时,除了昨天的那部分之外,还要单独导入一个context名称空间 --> <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:
http:
http:
<!-- 告知spring框架在通过读取配置文件创建容器时,扫描的包,并根据包中类的注解创建对象--> <context:component-scan base-
="com.baidu"></context:component-scan> </beans> 1.3.6 关于Spring注解和XML的选择问题 注解的优势 : 配置简单,维护方便(我们找到类,就相当于找到了对应的配置) XML的优势 : 修改时,不用改源码.不涉及重写编译和部署. Spring管理Bean方式的比较 : 基于XML配置 基于注解配置 Bean定义 <bean id="..."
=".."> @Component 衍生类@Repository @Service @Controller Bean名称 通过id或name指定 @Component("person") Bean注入 <property>或者通过p命名空间 @Autowired按类型注入 @Qualifier按名称注入 生命过程, init-method destroy-method @PostConstruct初始化 @PreDestroy销毁 @Scope设置作用范围 Bean作用范围 范围scope属性 适合场景 Bean来自第三方,使用其它 Bean的实现类由用户自己开发 1.5 spring的纯注解配置
@Configuration
@ComponentScan(basePackages = "com.baidu")
SpringConfiguration { } 那么新的问题又来了,我们如何获取容器呢?
Client {
main(String[] args) {
ApplicationContext ac =
AnnotationConfigApplicationContext(SpringConfiguration.
);
ICustomerService cs = (ICustomerService) ac.getBean("customerService"); cs.saveCustomer(); } } 1.5.3新注解说明 1.5.3.1@Configuration 作用: 用于指定当前类是一个spring配置类,当创建容器时会从该类上加载注解。获取容器时需要使用AnnotationApplicationContext(有@Configuration注解的类.
)。 属性: value:用于指定配置类的字节码 示例代码:
@Configuration
SpringConfiguration{ } 1.5.3.2@ComponentScan 作用: 用于指定spring在初始化容器时要扫描的包。作用和在spring的xml配置文件中的: <context:component-scan base-
="com.baidu"/>是一样的。 属性: basePackages:用于指定要扫描的包。和该注解中的value属性作用一样。 1.5.3.4@Import 作用: 用于导入其他配置类,在引入其他配置类时,可以不用再写@Configuration注解。当然,写上也没问题。 属性: value[]:用于指定其他配置类的字节码。 示例代码: @Configuration @ComponentScan(basePackages = "cn.baidu.spring") @Import({ Configuration_B.
})
Configuration_A { } @Configuration @PropertySource("classpath:info.properties")
Configuration_B { } 1.5.3.5@Bean 作用: 该注解只能写在方法上,表明使用此方法创建一个对象,并且放入spring容器。它就相当于我们之前在xml配置中介绍的factory-bean和factory-method。 属性: name:给当前@Bean注解方法创建的对象指定一个名称(即bean的id)。 示例代码: @Bean(name = "datasource2")
DataSource createDS()
Exception { ComboPooledDataSource comboPooledDataSource =
ComboPooledDataSource(); comboPooledDataSource.setUser("root"); comboPooledDataSource.setPassword("1234"); comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver"); comboPooledDataSource.setJdbcUrl("jdbc:mysql:///spring_ioc");
comboPooledDataSource; } 单元整合
@RunWith(value=SpringJUnit4ClassRunner.
) @ContextConfiguration(value="classpath:applicationContext.xml")
Demo1 {
@Resource(name="userService")
UserService userService; @Resource(name="userDao")
UserDao userDao;
@Test
run3(){ userService.save(); } @Test
run4(){
userDao.save(); } 使用XML方式完成IOC的入门 1. 导入jar包 2. 编写接口和实现类 3. 编写配置文件,管理实现类 4. 编写入门的程序 使用注解的方式完成IOC的入门 1. 导入jar包 2. 编写接口和实现类 3. 编写applicationContext.xml配置文件,目的 : 让注解生效 4. 在实现类上编写注解 5. 编写入门的程序 1. 管理类的 @Component 所有类都可以使用 @Controller Web层使用的注解,就是Action使用的. @Service 业务层使用的注解 @Repository 持久层使用的注解 2. 依赖注入的注解 (set方法可以省略不写) @Value 给普通类型属性注入值(String
) @Resource 给引用类型注入值的,强调 : 该注解的属性时name @Scope 对象作用范围,默认单例的. Spring整合WEB ServletContext对象,服务器启动就创建,服务器关闭就销毁. 监听器 : 监听ServletContext域对象的创建和销毁的. ServletContextListener监听器 , init destory 总结 : 服务器启动后,Spring创建对象,Spring创建Web版本的工厂,把工厂保存到ServletContext域对象中. 编写 : 从ServletContext对象中获取到Web版本的工厂,从工厂中获取到Service对象,调用方法.