一、对象创建相关注解
⚪搭建开发环境
<context:component-scan base-package="com.bao"></context:component-scan>
作用:让Spring框架设置包及其子包扫描对应的注解,使其生效。
⚪对象创建相关注解
@Component注解
替换原有spring配置文件中的<bean>标签
id属性 component注解 提供了默认的设置方式-单词首字母小写
@Component细节
1.显示指定工厂创建对象id值:@Component("u")
2.Spring配置文件可覆盖注解配置内容(id值要一致)
@Component的衍生注解:
1.@Repository
@Repository
public class UserDAO{
}
2.@Service
@Service
public class UserService{
}
3.@Controller
@Controller
public class RegAction{
}
作用、用法、细节与@Component完全一样
目的:更加准确的表达一个类型的作用。
注意:Spring整合Mybatis开发过程中 不使用@Repository、@Component
@Scope注解
作用:控制简单对象创建次数
注意:不添加@Scope Spring提供默认值singleton
以前写法:<bean id="" class="" scope="singleton|prototype"/>
@Lazy注解
作用:延迟创建单实例对象
注意:一旦使用了@Lazy注解后,Spring会在app.getBean()使用时创建对象
生命周期方法相关注解
1.初始化相关方法 @PostConstruct
InitializingBean接口
<bean init-method="自定义初始化方法"/>
2.销毁方法 @PreDestroy
DisposableBean接口
<bean destory-method="自定义销毁方法"/>
注意:1.上述两个注解不是Spring提供的,JSR(JavaEE规范)520提供
2.通过接口实现了接口的契约性
二、注入相关注解
用户自定义类型 @Autowired
@Autowired
(为UserService注入UserDAO,并调用userDAO的save方法)
@Service
public class UserServiceImpl implements UserService {
private UserDAO userDAO;
public UserDAO getUserDAO() {
return userDAO;
}
@Autowired
//@Qualifier("userServiceImpl")
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public void register() {
userDAO.save();
}
}
@Autowired细节:
基于类型(UserDAO)注入,若要基于名字注入,还需要引入@Qualifier("userServiceImpl")注解。
Autowired注解的放置位置:
1)对应成员变量的set方法上
2)直接成员变量上(反射注入)[推荐]
JavaEE规范中类似功能的注解:
JSR250的@Resouce(name="userDAOImpl"),基于名字进行注入;
JSR330的@Inject,作用与@Autowired完全一致,需要jar包导入;
JDK类型 @Value
开发步骤:
1.设置XXX.properties配置文件(key->value)
id = 50
name = chen
2.Spring配置文件引入
<context:property-placeholder location="classpath:XXX.properties"></context:property-placeholder>
3.对应类的变量加入Value注解
@Value("${id}")
private int id;
@Value("${name}")
private String name;
@PropertySource
作用:用于替换Spring配置文件中的<context:property-placeholder>标签
开发步骤:
1.设置XXX.properties配置文件(key->value)
id = 50
name = chen
2.应用@PropertySource
@Component
@PropertySource("classpath:xxx.properties")
public class Category {
@Value("${id}")
private int id;
@Value("${name}")
private String name;
3.对应类的变量加入Value注解
@Value("${id}")
private int id;
@Value("${name}")
private String name;
@Value细节:
1.@Value修饰的成员变量不能是Static
2.@Value+@PropertySource不能注入集合类型
解决方案:Spring提供新的配置形式 YAML YML(SpringBoot)
三、注解扫描详解
<context:component-scan base-package="com.bao"></context:component-scan>
扫描-->当前包 及其子包
1.排除方式(可叠加使用)
<context:component-scan base-package="com.bao">
<context:exclude-filte type="" expression=""/>
</context:component-scan>
type有:
assignable:排除指定类型,不进行扫描
<context:exclude-filter type="assignable" expression="com.bao.bean.User"/>
annotation:排除特定的注解,不进行扫描@Service
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
aspectj:排除切入点表达式(包、类切入点)
<context:exclude-filter type="aspectj" expression="com.bao.bean..*"/>
(了解)regex:正则表达式
(了解)custom:自定义排除策略
2.包含方式(可叠加使用)
<context:component-scan base-package="com.bao" use-default-filters="false">
<context:include-filte type="" expression=""/>
</context:component-scan>
use-default-filters="false"属性:Spring默认注解扫描方式失效
type有:
assignable:指定类型进行扫描
<context:include-filter type="assignable" expression="com.bao.bean.User"/>
annotation:特定的注解进行扫描@Service
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
aspectj:切入点表达式(包、类切入点)
<context:include-filter type="aspectj" expression="com.bao.bean..*"/>
(了解)regex:正则表达式
(了解)custom:自定义包含策略
四、对于注解开发的思考
配置互通
Spring注解配置 与 配置文件配置 互通
@Repository
public class UserDAOImpl{
}
public class UserServiceImpl{
peivate UserDAO userDAO;
set;get;
}
<bean id="userService" class="com.bao.bean.UserServiceImpl"
<property name="userDAO" ref="userDAOImpl"/>
</bean>
什么情况使用注解?什么情况使用配置文件?
自己开发的类上(UserService等) 可以使用注解
非自己开发的类(SqlSessionFactoryBean等) 不可以使用注解