什么是注解:
- 注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值…
- 使用注解,注解作用在类上面,方法上面,属性上面
-
Spring针对Bean管理中创建对象提供注解
@Component
- @Service
- @Controller
- @Repository
一、注解注入属性一个简单的案例
1)Spring配置文件引入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://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
2)Spring配置文件中开启注解扫描
<context:component-scan base-package="com.pc.service"/>
注意,在第一步必须要引入context约束,否则无法出现context标签。
3)在类上添加@Component注解
@Component("user")
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4)编写测试
ApplicationContext act = new ClassPathXmlApplicationContext("beans-ioc.xml");
User user = (User) act.getBean("user");
System.out.println(user);
二、Spring IoC常用注解详解
1)Bean标注注解
@Component :组件通用注解,常用于Model类
@Controller :常用于对Controller实现类进行标注
@Service:常用于对Service实现类进行标注
@Repository:常用于对DAO实现类进行标注
@Component是Spring提供的通用的组件注解。@Component、@Controller、@Service和@Repository功能一样,可以互换,我们使用不同注解主要为了区分被注解的类处在不同的业务层,使逻辑更加清晰。这四个注解主要是定义bean,创建bean。
它们使用方法:
(1)标注在类上
(2)@Component(“name”)等于@Component(value=”user”)
(3)@Component相当于@Component(“className”)
2)Bean属性注入注解
@Value :注入普通类型属性
@Resource :注入对象类型
@Autowired :注入对象类型,默认按照类型注入。结合@Qualifier注解完成按名称的注入。
(1)@Value注解
例如,为上面案例中User类中id和name一个初始值,我们可以在id和name属性上面使用@Value注解,也可以在它们的set方法上使用@Value注解
@Component(value="user")
public class User {
@Value("1")
private Integer id;
@Value("lzgsea")
private String name;
public Integer getId() {
return id;
}
//@Value("1")
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
//@Value("lzgsea")
public void setName(String name) {
this.name = name;
}
}
(2)@Resource :注入对象类型
public class UserTest {
@Resource(name = "user")
private User user;
}
(3)@Autowired注解
public class UserTest {
// 按名注入,需要组件设置名称
// @Resource(name = "user")
// 和上面功能一样,按名注入
@Qualifier("user")
@Autowired
private User user;
}
3)Bean的作用范围注解@Scope
Bean的范围的注解:默认是单例的。
@Scope :在类上添加的,控制类生成的时候采用单例还是多例。
@Scope取值:
singleton :单例
prototype :多例
request :request域,需要在web环境
session :session域,需要在web环境
application: context域,需要在web环境
globalsession 集群环境的session域,需要在web环境
4)Bean的生命周期注解
@PostConstruct :相当于init-method
@PreDestroy :相当于destroy-method
这两注解用在方法上面。
@PostConstruct
public void init() {
System.out.println("初始化方法......");
}
@PreDestroy
public void destroy() {
System.out.println("销毁方法......");
}