第1章 基于注解的IOC配置
1.1 写在最前
学习基于注解的IoC配置,大家脑海里首先得有一个认知,即注解配置和xml配置要实现的功能都是一样的,都是要降低程序间的耦合。只是配置的形式不一样。
一、IOC注解入门
1、创建项目,并添加jar包 (7个 )
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://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
</beans>
3、创建类,并配置注解
自己创建
4、配置包扫描器
<!-- 告知spring框架在读取配置文件,创建容器时,扫描注解,依据注解创建对象,并存入容器中 -->
<context:component-scan base-package="com.csq"/>
5、测试
二 与IOC相关的注解(重点)
1. @Component
作用:
把资源让spring来管理。相当于在xml中配置一个bean。
属性:
value:指定bean的id。如果不指定value属性,默认bean的id是当前类的类名。首字母小写。
2. @Controller @Service @Repository
他们三个注解都是针对一个的衍生注解,他们的作用及属性都是一模一样的。
他们只不过是提供了更加明确的语义化。
@Controller:一般用于表现层的注解。
@Service:一般用于业务层的注解。
@Repository:一般用于持久层的注解。
细节:如果注解中有且只有一个属性要赋值时,且名称是value,value在赋值时可以不写。
三、与DI相关的注解
1. @Autowired(重点)
作用:
自动按照类型注入。当使用注解注入属性时,set方法可以省略。它只能注入其他bean类型。当有多个类型匹配时,使用要注入的对象变量名称作为bean的id,在spring容器查找,找到了也可以注入成功。找不到就报错。
2. @Qualifier
作用:
在自动按照类型注入的基础之上,再按照Bean的id注入。它在给字段注入时不能独立使用,必须和@Autowire一起使用;但是给方法参数注入时,可以独立使用。
属性:
value:指定bean的id。
3. @Resource
作用:
直接按照Bean的id注入。它也只能注入其他bean类型。
属性:
name:指定bean的id。
4. @Value
作用:
注入基本数据类型和String类型数据的
属性:
value:用于指定值
四.其他注解
1.@Scope(重点)
作用:
指定bean的作用范围。
属性:
value:指定范围的值。
取值:singleton prototype
2.@PostConstruct
作用:
用于指定初始化方法。
3. @PreDestroy
作用:
用于指定销毁方法。
五.纯注解开发
1. 使用注解配置要扫描的包
@Configuration//表明当前类是一个配置类
@ComponentScan(basePackages = "com.csq")//配置要扫描的包
public class SpringConfiguration {
}
有xml时候
这一步就相当于<!-- 告知spring框架在读取配置文件,创建容器时,扫描注解,依据注解创建对象,并存入容器中 -->
<context:component-scan base-package="com.csq"/>
2.获取容器:由于我们已经没有了xml文件,所以再用读取xml方式就不 能用了
这时需要指定加载哪个类上的注解
ApplicationContext ac =
new AnnotationConfigApplicationContext(SpringConfiguration.class)
有xml时候
这一步就相当于
ApplicationContext ac =
new ClassPathXmlApplicationContext("applicationContext.xml");
3.注解说明
3.1 @Configuration
作用:
用于指定当前类是一个配置类,会从该类上加载注解。读取该类上@ ComponentScan注解初始化spring容器。
3.2 @ComponentScan
作用:
用于指定spring在初始化容器时要扫描的包。
属性:
basePackages:用于指定要扫描的包。和该注解中的value属性作用一样。
3.3 @Bean
作用:
该注解只能写在方法上,表明使用此方法创建一个对象,并且交给spring管理。
属性:
name:给当前@Bean注解方法创建的对象指定一个名称(即bean的id)。
示例代码:
@Bean(name = "datasource2")
public DataSource C3P0Utils() throws Exception {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("1234");
comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
comboPooledDataSource.setJdbcUrl("jdbc:mysql:///spring_ioc");
return comboPooledDataSource;
}
注意:在spring4.3以前都需要提供一个占位符配置器:
PropertySourcesPlaceholderConfigurer
而在spring4.3以后,则不需要提供。
提供的方式如下:(在SpringConfiguration或C3P0Utils中配置均可)
@Bean
public static PropertySourcesPlaceholderConfigurer
propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
为了我们以后维护方便,开发中大多数用下面这种方式
@PropertySource("classpath:jdbc.properties") // 加载jdbc.properties文件到Spring容器中去
public class C3P0Utils {
@Value("${jdbc.driver}")
private String driverClass;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.user}")
private String user;
@Value("${jdbc.password}")
private String password;
@Bean(name = "dataSource")
public DataSource createDataSource() throws PropertyVetoException {
ComboPooledDataSource datasource = new ComboPooledDataSource();
datasource.setDriverClass(driverClass);
datasource.setJdbcUrl(url);
datasource.setUser(user);
datasource.setPassword(password);
return datasource;
}
在src下面创建一个properties文件,在里面配置好,这样换了数据库或者密码只需要在文件中改,代码不需要动;
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///test11
jdbc.user=root
jdbc.password=root