Spring-注解

1. 使用注解代替xml文件

  1. 为新的配置文件导入新的命名空间(约束)

    spring-context-4.3.xsd

  2. 开启使用注解代理配置文件

    <?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>
    
  3. 导入lib包

    1. 本文需要使用的lib包:
      1. com.springsource.org.apache.commons.logging-1.1.1.jar
      2. com.springsource.org.apache.log4j-1.2.15.jar
      3. hamcrest-all-1.3.jar
      4. junit-4.12.jar
      5. spring-aop-4.3.9.RELEASE.jar
      6. spring-beans-4.3.9.RELEASE.jar
      7. spring-context-4.3.9.RELEASE.jar
      8. spring-core-4.3.9.RELEASE.jar
      9. spring-expression-4.3.9.RELEASE.jar
      10. spring-test-4.3.9.RELEASE.jar
  4. 在类中使用注解

    1. 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 + "]";
          }
      }
      
    2. 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("销毁方法");
          }
      }
      
    3. 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);
          }
      }
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值