• spring再学习之注解


    1、使用注解配置spring

    <?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"
    xmlns:tx
    ="http://www.springframework.org/schema/tx"
    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
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.2.xsd "
    > <!-- 指定扫描cn.itcast.bean及其子孙包 --> <context:component-scan base-package="cn.itcast"></context:component-scan> </beans>
    component:组件
    2、在类中使用注解
    /**
     * 
     * @author zws
     *@Component("user")
     *含义就是下面
     *<bean name="user" class="cn.itcast.bean.User"  />
     */
    /*
     * 
     * @Service("user") // service层
     *  @Controller("user") // web层
     *    @Repository("user")// dao层
     */
     @Repository("user")// dao层
    //指定对象的作用范围,单例多例
    //@Scope(scopeName="singleton")
    public class User {
        private String name;
        @Value("18")
        //通过反射的Field赋值,破坏了对象的封装性
        private Integer age;
        /*
        @Autowired //对象,自动装配----对应另一个@Component("car")
        问题:如果匹配多个类型一致的对象.将无法选择具体注入哪一个对象.
        此时要配合@Qualifier使用
        @Qualifier("car2")//使用@Qualifier注解告诉spring容器自动装配哪个名称的对象
        */
        @Resource(name="car2")//手动注入,指定注入哪个名称的对象
        private Car car;
        
        public Car getCar() {
            return car;
        }
        public void setCar(Car car) {
            this.car = car;
        }
        public String getName() {
            return name;
        }
        @Value("tom")
        //set方法, 将name注入
        public void setName(String name) {
            this.name = name;
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
        @PostConstruct //在对象被创建后调用.init-method
        public void init(){
            System.out.println("我是初始化方法!");
        }
        @PreDestroy //在销毁之前调用.destory-method
        public void destory(){
            System.out.println("我是销毁方法!");
        }
        @Override
        public String toString() {
            return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
        }
        
    }

    2、与junit整合测试

    包,四个核心包+日志包+aop+test

    /**
     * @RunWith :帮我们创建容器
     * @ContextConfiguration :指定创建容器时使用哪个配置文件
     * @author zws
     *
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:cn/itcast/e_annotationaop/applicationContext.xml")
    public class Demo {
        
        @Resource(name="userServiceTarget")  
        private UserService us;
        
        @Test
        public void fun1(){
            us.save();
        }
    
    }
  • 相关阅读:
    LeetCode-380 Insert Delete GetRandom O(1)
    LeetCode-378 Kth Smallest Element in a Sorted Matrix
    LeetCode-373 Find K Pairs with Smallest Sums
    LeetCode-372 Super Pow
    LeetCode-365 Water and Jug Problem
    LeetCode-352 Data Stream as Disjoint Intervals
    LeetCode-347 Top K Frequent Elements
    LeetCode-341 Flatten Nested List Iterator
    LeetCode-337 House Robber III
    【问题】win10开机慢 底部任务栏出来慢
  • 原文地址:https://www.cnblogs.com/xiaonantianmen/p/9176330.html
Copyright © 2020-2023  润新知