Spring学习(七)Spring IOC中的注解开发
spring IOC中的注解开发
JDK1.5支持了注解,spring2.5就支持注解了。然后如果是在spring4之后,还需要导入aop的包才能使用注解开发。
要使用注解: 文档1.9:
1.导入context注解约束:xmlns:context="http://www.springframework.org/schema/context"
2.配置注解的支持:<context:annotation-config/>
<?xml version="1.0" encoding="UTF-8"?>
<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">
<context:annotation-config/>
</beans>
@Autoware
直接在属性上使用即可,这样可以直接自动装配。也可以写在属性对应的set方法上。并且因为注解是基于反射的,如果在属性上使用,可以连set方法都省略掉。但是使用注解必须要被自动装配的属性在ioc容器中(也就是xml文件里要有对应属性的bean)存在。如果显示定义了@Autoware(required=false)表示允许该属性在容器中不存在。如果自动装配的环境十分复杂(比如类型和名称都存在歧义),那么可以组合@Qualifier(value=“bean的id”)来使用。
在类中的cat和dog属性加上该注解
public class Person {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
}
然后xml文件里注册cat和dog类的bean
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!--开启注解的支持-->
<context:annotation-config/>
<bean id="cat" class="com.hj.pojo.Cat"/>
<bean id="dog" class="com.hj.pojo.Dog"/>
<bean id="person" class="com.hj.pojo.Person"/>
</beans>
@Resource注解
和@Autoware类似
public class Person {
@Autowired
private Cat cat;
@Resource(name = "dog")
private Dog dog;
private String name;
}
@Nullable
@Nullable 字段标记的字段,说明这个字段可以为null 比如你@Nullable String name,那么这个name就可以为null。
@Component
@Compnent将类变成一个组件,可以省略xml配置文件中bean的注册,默认的注册名为该类的小写字母。
在xml文件中添加:<context:component-scan base-package="com.hj.pojo"/>
这样在com.hj.pojo下的组件注解就会生效了。
//@Component 等价于<bean id="user" class="com.hj.pojo.User"/>
@Component
public class User {
public String name;
}
那么如何给User类注入值呢?
可以组合@Value使用,@Value也可以放在set方法上,也可以放在属性上
比如:
//@Component 等价于<bean id="user" class="com.hj.pojo.User"/>
@Component
public class User {
//相当于<property name="name" value="张三"/>
@value("张三")
public String name;
}
另外,还有几个注解的功能是一样的,但是应用的地方不一样的注解。
@Repository是dao层的@Component,@Service是service层的@Component,@Controller是controller层的@Component。注意使用这些组件注解都要在xml文件里使用component-scan扫描对应的包。
还可以组合@Scope(“作用域”)来设置该组件中bean对应的作用域。
@Configuration
@Configuration和@Bean组合可以取代xml文件配置:
@Configuration
public class AppConfig{
@Bean
public MyService myService(){
return new MyServiceImpl();
}
}
就等价于:
<beans>
<bean id="myService" class="com.hj.services.MyServiceImpl"/>
</beans>
我们重新写一个类User.java
public class User {
@Value("张三")
public String name;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
然后再写一个配置类Myconfig.java
@Configuration
public class MyConfig {
@Bean //注册一个bean,就相当于之前写的一个bean标签,id=这个方法的方法名。
public User getUser(){
return new User();
}
}
这样我们的User类也被spring容器托管了,bean中的对象名为getUser。
不写xml文件了,直接上测试类:
public class MyTest {
public static void main(String[] args) {
//通过纯注解配置,需要使用AnnotationConfig来获取上下文,通过配置类的class对象加载
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
User getUser = context.getBean("getUser", User.class);
System.out.println(getUser.toString());//User{name='张三'}
}
}
小结
xml和注解:
- xml比较万能,适用于任何场合,维护简单
- 注解不是自己的类无法使用,维护相对复杂
- 一般而言:xml用来管理bean,注解只负责属性的注入。
本章节代码:spring-08,spring-09是实现@Configuration的