Spring-注解
1. 使用注解代替xml文件
-
为新的配置文件导入新的命名空间(约束)
spring-context-4.3.xsd
-
开启使用注解代理配置文件
<?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:p="http://www.springframework.org/schema/p" 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.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd "> <!-- 开启扫描com.ccblogs.java包(包括所有子包)下所有类中的注解 --> <context:component-scan base-package="com.ccblogs.java"></context:component-scan> </beans>
-
导入lib包
- 本文需要使用的lib包:
- com.springsource.org.apache.commons.logging-1.1.1.jar
- com.springsource.org.apache.log4j-1.2.15.jar
- hamcrest-all-1.3.jar
- junit-4.12.jar
- spring-aop-4.3.9.RELEASE.jar
- spring-beans-4.3.9.RELEASE.jar
- spring-context-4.3.9.RELEASE.jar
- spring-core-4.3.9.RELEASE.jar
- spring-expression-4.3.9.RELEASE.jar
- spring-test-4.3.9.RELEASE.jar
- 本文需要使用的lib包:
-
在类中使用注解
- Values.java类
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("values") public class Values { @Value("key1") private String key; @Value("value1") private String value; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } @Override public String toString() { return "Values [key=" + key + ", value=" + value + "]"; } }
- ComboList.java类
// 将对象注册到容器 // 相当于 <bean name="comboList" class="com.ccblogs.spring" /> // import org.springframework.stereotype.Component; // @Component("comboList") // import org.springframework.stereotype.Service; // @Service("comboList") // service层 // import org.springframework.stereotype.Controller; // @Controller("comboList") // web层 // import org.springframework.stereotype.Repository; // @Repository("comboList") // dao层 // 指定对象的作用范围(scope) // import org.springframework.context.annotation.Scope; // @Scope(scopeName="singleton") public class ComboList { // 给属性注入值(两种情况) // 1、在声明变量前加@声明,通过反射的Field赋值,破坏了封住性 // 2、在变量set方法前加@声明,通过set方法赋值 // import org.springframework.beans.factory.annotation.Value; // @Value("ccblogs") private String name; // 注解只有一个值并且属性是value,可以省略属性 // @Value("18") = @Value(value="18") private Integer age; // 引用对象注入(自动装配) // import org.springframework.beans.factory.annotation.Autowired; // @Autowired // import javax.annotation.Resource; // import org.springframework.beans.factory.annotation.Qualifier; // 注意:多值注入时需要以xml的方式实现多值,并使用@Autowired和@Qualifier("values")或者直接使用@Resource(name="values") private Values value; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Values getValue() { return value; } public void setValue(Values value) { this.value = value; } @Override public String toString() { return "ComboList [name=" + name + ", age=" + age + ", value=" + value + "]"; } // 初始化方法(在构造方法后调用) // import javax.annotation.PostConstruct; // @PostConstruct public void init() { System.out.println("初始化方法"); } // 销毁方法(在销毁方法前被调用 // import javax.annotation.PreDestroy; // @PreDestroy // 注意:作用域是singleton时,才能被执行 public void destory() { System.out.println("销毁方法"); } }
- Spring_test.java类
import javax.annotation.Resource; import org.junit.Test; import com.ccblogs.spring.ComboList; // 创建容器 // import org.junit.runner.RunWith; // import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; // @RunWith(SpringJUnit4ClassRunner.class) // 指定创建容器时的配置文件 // import org.springframework.test.context.ContextConfiguration; // @ContextConfiguration("classpath:applicationContext.xml") public class Spring_test { @Resource(name="comboList") private ComboList c; @Test public void test1() { System.out.println(c); } }
- Values.java类