• 基于Annotation配置Bean


      上一节讲了用xml文件的方式配置Bean,虽然可以满足所有要求,但是简单的两个类就配置了那么多内容。后期维护起来很不方便。这一节学习通过注解的方式实现Bean的配置。这里先了解下各个注解代表的含义

      @controller 控制器(注入服务)

      @service 服务(注入dao)

      @repository dao(实现dao访问)

      @component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>) 其中id默认是类小写第一个字母

        一般写成  @Component(value="cartest")

      上述这四个注解其实没有本质的区别都是实例化到spring容器中

      @Autowired 注解对Bean的属性变量、属性的Setter方法及构造函数进行标注,配合对应的注解处理器AutowiredAnnotationBeanProcessor完成Bean的自动配置工作。@Autowired注解默认是按Bean类型进行装配的,也就是说注解处理器AutowiredAnnotationBeanProcessor会在Spring容器中寻找与@Autowired注解所在属性同类型的Bean实例进行装配,如果找到多个满足条件的Bean实例时,将会抛出NoSuchBeanDefinitionException异常。@Autowired注解加上@Qualifier注解的话,可以直接指定一个Bean实例名称来进行装配。在实例应用中,不推荐使用@Autowired注解,因为该注解应用不当,会引发一些莫名其妙的问题

    要想实现注解配置要满足以下要求

     1、在Spring配置文件中定义context命名空间与相应的schemaLocation

    <?xml version="1.0" encoding="UTF-8"?>  
    <beans  
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
    ……  
    </beans> 

     2、配置扫描的路径  

        <context:component-scan base-package="ioc" />

     3、类前使用component

    package ioc;
    
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    import org.springframework.stereotype.Component;
    @Component
    public class User {
        private String name;
        private int age;
        private String sex;
        private Car car;
    
        //默认构造方法
        public User() {
        }
        // 有参构造方法
        public User(String name, Car car, List<Car> carList) {
            super();
            this.name = name;
            this.car = car;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
        public Car getCar() {
            return car;
        }
    
        public void setCar(Car car) {
            this.car = car;
        }
    
        public void init() {
            System.out.println("开始调用");
        }
        public void destroy() {
            System.out.println("结束调用");
        }
    
    }

    4、依赖的类配置@Autowired

    package ioc;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    public class Car {
        private String name;
        private double price;
        @Autowired
        public Car() {
        }
    
        public Car(String name, double price) {
            super();
            this.name = name;
            this.price = price;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public double getPrice() {
            return price;
        }
    
        public void setPrice(double price) {
            this.price = price;
        }
    
        @Override
        public String toString() {
            return "Car [name=" + name + ", price=" + price + "]";
        }
    
    }

    5、新建testmain类测试 

    package ioc;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestMain {
        public static void main(String[] args) {
            //获取容器对象
         ApplicationContext ac=new ClassPathXmlApplicationContext("ioctest.xml");
             //通过容器获取配置的javabean
         User car1=(User)ac.getBean("user");
         System.out.println(car1);
    
        }
    
    }

      

      

      

      

  • 相关阅读:
    TP-LINK WR941N路由器研究
    thinkjs初试
    记浏览器帐号登录插件开发遇到的问题
    你被adblock坑过吗?
    web应用,我们需要了解什么?
    算法之合并排序
    算法之插入排序
    算法之初体验
    nodejs学习笔记之网络编程
    炫酷吊炸天的nodeppt
  • 原文地址:https://www.cnblogs.com/ysmdbk/p/11399654.html
Copyright © 2020-2023  润新知