【spring学习笔记二】使用注解配置spring

注解使用最多的框架就是spring框架,因此学习spring时学会注解配置也很重要。本次笔记就是本人学习注解配置时的笔记,供大家参考。

准备工作:

第一步:创建一个项目。

第二步:导包(4+2)  新版本要加入spring-aop-4.2.4.RELEASE.jar包

第三步:准备对象bean(User类,Car类)

第四步:准备配置文件applicationContext.xml

【spring学习笔记二】使用注解配置spring

接下来开始注解配置

第一步:需要为主配置文件引入新的命名空间(context)。

第二步:开启使用注解代替xml文件配置spring。(相当于打开开关)

<context:component-scan base-package=""></context:component-scan>(根据包名自动扫描对象)

xml文件源代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
<!-- 注解开关,自动扫描包下或者包下所有子包的类中的注解 -->
<context:component-scan base-package="spring.bean"></context:component-scan>
<bean name="car2" class="spring.bean.Car">
<property name="name" value="兰博基尼"></property>
<property name="color" value="黑色"></property>
</bean>
</beans>

第三步:在类中完成注解,实现配置。

A.注册bean注解

@Component(“user”)(翻译:组件) 相当于xml文件中<bean name="user" class="spring.bean.User">注册bean

放在类名前面 表示把该类配置到spring容器中。通过(“user”)表示该类在spring容器中叫什么名字,相当于之前xml文件配置的name属性。

Component是作为spring早期的一个注解,随着spring的发展逐渐有了替代的注解如:

@Service(“user”)--》service层

@Controller(“user”)--》web层

@Repository(“user”)--》dao层

以上三个注册bean注解和Component注解作用是一样的,只不过是考虑分层思想,一看就知道是哪一层的对象

B.确定单例或者多例的注解

@Scope(scopeName="singleton|prototype");

放在类名前面,确定对象的作用域,也就是该类是单例singleton还是多例prototype。

C.利用注解实现关于属性值的注入

@Value(“值”)

该注解可以放在成员变量属性定义的上面,用来对该属性赋值(通过反射的方法赋值,破坏了对象的封装性)。

也可以把该注解加在set该属性的方法上。(推荐)

D.给对象赋值

首先把另外一个对象Car类使用@Component注解配置到spring容器中,然后最简单的方法就是在User类中的Car属性声明上使用@Autowired注解即可。自动在spring容器中寻找相同的类型的对象进行配置。缺点就是如果匹配到多个想类型相同的对象,无法选择哪一个。因此可以在@Autorired注解后可以增加@Qualifier("car2")注解确定自动装配的名字。

也可以直接指名道姓的使用一个注解完成手动注入@resource,指定注入哪个对象。(推荐使用这种方式)

@Resource(name="car2")

E.使用注解完成对象的初始化和销毁,注意只能用于单例的对象,因此Scope为singleton。

初始化方法,在方法前面加注解

@PostConstruct//在对象被创建后调用,相当于init-method方法

销毁方法,在方法前面加注解

@PreDestroy //在对象销毁之前调用的 相当于destroy-method


User类:

package spring.bean;


import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;


@Component("user")
@Scope(scopeName="singleton")
public class User {
@Value("18")
private Integer age;
@Value("Tom")
private String name;
//@Autowired
//@Qualifier("car2")
@Resource(name="car")
private Car car;
@PostConstruct//在对象被创建后调用,相当于init-method方法
public void init(){
System.out.print("初始化方法");
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@PreDestroy //在对象销毁之前调用的 相当于destroy-method
public void destroy(){
System.out.print("销毁方法");
}
@Override
public String toString() {
return "User [age=" + age + ", name=" + name + ", car=" + car + "]";
}

}


Car类:

package spring.bean;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Component("car")
public class Car {
@Value("宝马")
private String name;
@Value("yellow")
private String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Car [name=" + name + ", color=" + color + "]";
}

}

测试类:

package spring.test;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import spring.bean.User;


public class Fun {
@Test
public void fun1(){
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User user1 = (User) ac.getBean("user");
System.out.println(user1);
ac.close();
}
}


spring 的注解大致也就以上一些东西,个人觉得使用注解进行配置更加容易查看,在企业实际开发中往往使用注解和xml配置文件方式共同装配bean。