spring基于注解的IOC和案例
1 IOC常用注解
Spring IOC容器:Map结构
@Resource报错:java.lang.String javax.annotation.Resource.lookup()
解决:pom.xml增加
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.1</version>
</dependency>
常用注解
1 @Component: 把当前对象存入spring容器中
属性: value:用于指定bean的ID。不写时默认值当前类名且首字母小写
2
@Controller 表现层
@Service 业务层
@Repository 持久层
以上三个作用和属性和@Component是一样的 spring框架提供明确的三层使用注解 是三层对象更加清晰
相当于:
3 @Autowired
自动按照类型注入。当使用注解注入属性时,set方法可以省略。它只能注入其他 bean 类型。当有多个 类型匹配时,使用要注入的对象变量名称作为 bean 的 id,在 spring 容器查找,找到了也可以注入成功。找不到 就报错
自动按照类型注入。只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功
位置:变量或者方法
细节: 使用注解注入时set方法不是必须的
如果IOC容器中没有任何bean的类型和注入的变量类型匹配 则报错
如果IOC容器中有多个类型
4 @Qualifier 作用: 在自动按照类型注入的基础之上,再按照 Bean 的 id 注入。它在给字段注入时不能独立使用,必须和 @Autowire 一起使用;但是给方法参数注入时,可以独立使用。 属性: value:指定 bean 的 id。
按照类中注入的基础上再按照名称注入。给类成员注入不能单独使用但是给方法参数注入可以。
属性 value 用于指定注入bean的ID
5 @Resource
作用: 直接按照 Bean 的 id 注入。它也只能注入其他 bean 类型。
属性: name:指定 bean 的 id
6 @Value 用于注入基本类型和String类型
属性 value 用于指定数据的值 可以使用spring中的spel (spring的el表达式)
spel写法:${表单式}
7 用于改变作用域范围
在bean标签在scope属性
@Scope 指定bean的作用范围
属性:value: singleton prototype
8 和生命周期相关 在bean标签在init-method和destroy-method属性
@PreDestroy 指定销毁方法
@PostConstruct 指定初始化方法
使用XML方式和注解方式实现单表CURD操作
持久层 dbutils
改造基于注解IOC案例
spring新注解
注解类型是数组有且只有一个 可以不写{}
1 Configuration 指定当前类是个配置类 细节:
当配置类作为AnnotationConfigApplicationContext对象创建的参数是 该注解可用不写
2 ComponentScan
用于通过注解指定spring创建容器是要扫描的包 @AliasFor 别名
value 和 basePackages 作用一样 指定创建容器是要扫描的包
此种注解等于beam.xml中配置<context:component-scan base-package="com"></context:component-scan>
3 @Bean
作用:用于把当前方法的返回值作为bean对象存入spring的ioc容器中
属性: name:用于指定bean的ID 默认值 当前方法的名称
使用注解配置 如果方法有参数 spring框架去容器中查找有没有可用的bean对象
查找的方式和Autowired注解是一样的
4 PropertySource 用于指定properties文件位置
属性:value:文件名称和文件路径classPath 类路径下
5 Import 导入其他配置类
属性:value 用于指定其他配置类的字节码
使用import注解之后 有import注解的类是主配置类 导入的是子配置类
例子:
//@Configuration
@ComponentScan(basePackages = {"com"} )
@Import(JdbcConfig.class)
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfig {
}
//@Configuration
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
/**
* 创建一个QueryRunner对象
* @param dataSource
* @return
*/
@Bean(name = "runner")
@Scope(value = "prototype")
public QueryRunner createQueryRunner(DataSource dataSource) {
return new QueryRunner(dataSource);
}
/**
* 创建数据源对象
* @return
*/
@Bean(name = "dataSource")
public DataSource createDataSource () {
try {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setDriverClass(driver);
comboPooledDataSource.setJdbcUrl(url);
comboPooledDataSource.setUser(username);
comboPooledDataSource.setPassword(password);
return comboPooledDataSource;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
实际开发:
注解和XML结合使用: 自己实现的类 使用注解 jar包 使用XML
一个对象多个实现类 方法上参数添加@Qualifier("")
spring和Junit整合
1 使用@RunWith 注解替换原有运行器 @RunWith(SpringJUnit4ClassRunner.class) public class AccountServiceTest { }
2 使用@ContextConfiguration 指定 spring 配置文件的位置
@ContextConfiguration 注解:
locations 属性:用于指定配置文件的位置。如果是类路径下,需要用 classpath:表明
classes 属性:用于指定注解的类。当不使用 xml 配置时,需要用此属性指定注解类的位置。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:bean.xml"}) public class AccountServiceTest { }
3 使用@Autowired 给测试类中的变量注入数据
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:bean.xml"})
public class AccountServiceTest { @Autowired
private IAccountService as ; }