- 引入注解开发jar包
在Spring4之后,想要使用注解形式开发,则需要有aop包的支持
- 在配置文件applicationContext.xml中添加约束信息
有了aop包的支持后,还需要在配置文件中引入context约束
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
因此添加完context约束后,配置文件applicationContext.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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>
- 配置组件扫描
在配置文件中声明哪个包下的类型使用组件扫描,即在配置文件中添加下面一句话
<context:component-scan base-package="com.ph.demo1"/>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
<!--base-package:使用组件扫描的包,表示该包下的类可以使用组件扫描-->
<context:component-scan base-package="com.ph.demo1"/>
</beans>
- 在类中添加注解
在student类中添加注解@Component("student")
@Component("student")//相当于bean里的id
public class Student {
private String name;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
'}';
}
}
- 使用注解注入属性
对于普通属性值,在使用注解注入属性时可以不用提供set方法,直接在属性名上添加@Value(“值”)
对于对象类型的属性值,使用@Autowired。直接使用这种方式,是按照类型完全属性注入,也可以使用@Resource(name=“id”),id为对象类型的id。
在cat类的name属性上添加@Value("red")
@Component("cat")
public class Cat {
@Value("red")
private String name;
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
'}';
}
}
在student类的name属性上添加@Value("属性注入")、在cat上添加@Autowired
@Component("student")//相当于bean里的id,下面的类则是bean里的class
public class Student {
@Value("属性注入")//相当于配置bean时的property
private String name;
@Autowired//也可以替换成@Resource(name="cat")
private Cat cat;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
'}';
}
}
测试类
public class test {
public static void main(String[] args){
ApplicationContext applicationContext= new ClassPathXmlApplicationContext("applicationContext.xml");
Student stu = (Student)applicationContext.getBean("student");
System.out.println(stu);
}
}
运行结果
- @Component说明
为了更好的进行分层,Spring可以使用其它三个注解,功能类似。让后期维护时,可以看到注解时就知道类是属于哪个层级上的逻辑控制,这三个注解分别是@Controller web层 、@Service service层、@Repository dao层
- bean的各个属性与注解的对应关系
<bean id=”xxx” class=”Xxx” init-method=”aaa” destory-method=”bbb” scope=”singleton”/>
Id:@Component(“xxx”)/@Controller(“xxx”)/@Service(“xxx”)/@Repository(“xxx”)在类上添加
Calss:@Component(“xxx”)/@Controller(“xxx”)/@Service(“xxx”)/@Repository(“xxx”)下面的类
init-method:@PostConstruct,在方法上添加
destory-method:@PreDestory,在方法上添加
Scope:@Scope(“singleton”);,在类上添加
示例:
Student类
@Component("student")//相当于bean里的id
@Scope("singleton")
public class Student {
@Value("属性注入")
private String name;
@PostConstruct
public void init(){
System.out.println("初始化");
}
@PreDestroy
public void destory(){
System.out.println("销毁");
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
'}';
}
}
测试类
public class test {
public static void main(String[] args){
ApplicationContext applicationContext= new ClassPathXmlApplicationContext("applicationContext.xml");
Student stu = (Student)applicationContext.getBean("student");
System.out.println(stu);
//工厂关闭,调用销毁方法
((ClassPathXmlApplicationContext)applicationContext).close();
}
}
运行结果
- 配置文件applicationContext.xml与注解比较
xml可以使用任何场景,结构清晰、维护方便
注解不是自己提供的类使用不了,但是开发简单方便
- 配置文件applicationContext.xml与注解相结合
在实际的开发过程中,我们一般使用配置文件applicationContext.xml与注解相结合的方式进行开发,用xml管理Bean,使用@ Value@ Resource@ Autowired@ Qulifier注解完成属性注入。在使用过程中,可以不用扫描,因为扫描只是为了得到类上的注解,在配置文件中声明使用注解<context:annotation-config/>完成属性注入
因此我们在applicationContext.xmlpeizhi配置如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
<context:annotation-config/>
<bean id="student" class="com.ph.demo1.Student"/>
<bean id="cat" class="com.ph.demo1.Cat"/>
</beans>