spring 自动装配

autowired 自动注入 

 先说 bean节点 的 

autowire  属性 Byname bytype 翻译一下,一个就是靠名字 一个就是靠类型 

由于一点点原因,这里下回仔细补充,我写的示例丢了。

e m m m m

现在来讲注释标签

先来一个简单的 ,就一个类的

先在我们的配置文件 applicationcontext中的头文件加入
xmlns:context="http://www.springframework.org/schema/context" 
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

使用注解标签,配置文件先要能识别他 , 所以 , 得加上面的东西 , 识别的路径 ,现在开始写

 <context:component-scan base-package="cn.autowired.entity"></context:component-scan>
package为使用注解的路径 

现在来写类

@Component("car")
public class Car {
    @Value("兰博基尼")
    private String name;
    @Value("1")
    private  int age;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

以上就是一个简单的类了 , 

@Component("car")  getbean通过它输出值 
  @Value("兰博基尼")  为属性赋值 
 都在上面写

现在写测试类

    @Test
    public void ccd(){
        ApplicationContext context=new ClassPathXmlApplicationContext("application-03.xml");

       Car c= (Car)context.getBean("car");

        System.out.println(c.getName());
    }

     通过 获取

@Component("car") 里面的值

来拿取对象,然后我还输出了一下

现在看运行后的控制台

三月 07, 2018 10:38:57 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2280cdac: startup date [Wed Mar 07 22:38:57 CST 2018]; root of context hierarchy
三月 07, 2018 10:38:57 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [application-03.xml]
兰博基尼

现在来说 , 在另一个类中写入另一个对象的类 ,来 通过 注解 来获取值

现在看我的类
@Component("stau")
public class Student {

    @Value("xxx")
    private  String name;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Value("18")
    private Integer age;

    //@Resource(name = "car")
    @Autowired()
   @Qualifier(value = "car")
    private  Car ca;



    public Car getCa() {
        return ca;
    }

    public void setCa(Car ca) {
        this.ca = ca;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
处了上面见过的注解标签 ,还有 
 //@Resource(name = "car")
    @Autowired()
   @Qualifier(value = "car")  
以上标签为在另一个类中对象中赋值,

1
@Resource(name = "car") 

在对象属性上写 name的值为 那个类的
@Component("xxx") 中的值 ,就这样,值就装配上去了 

现在看测试类,以及控制台

@Component("stau")
public class Student {

    @Value("xxx")
    private  String name;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Value("18")
    private Integer age;

    @Resource(name = "car")
    //@Autowired()
  // @Qualifier(value = "car")
    private  Car ca;



    public Car getCa() {
        return ca;
    }

    public void setCa(Car ca) {
        this.ca = ca;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
 
    @Test
    public void as(){
        ApplicationContext context=new ClassPathXmlApplicationContext("application-03.xml");
        Student stu=     (Student)context.getBean("stau");
        System.out.println(stu.getName());
        System.out.println(stu.getCa().getName());

    }




三月 07, 2018 10:47:45 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2280cdac: startup date [Wed Mar 07 22:47:45 CST 2018]; root of context hierarchy
三月 07, 2018 10:47:45 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [application-03.xml]
xxx
兰博基尼

不仅把本类中的值输出,也将控过装配

@Resource(name = "car")  拿到了另一个类的值 



现在说
 @Autowired()
   @Qualifier(value = "car") 的这种方法 ,  Value 也是那边那个类的
@Component中的值  

@Component("stau")
public class Student {

    @Value("xxx")
    private  String name;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Value("18")
    private Integer age;

   // @Resource(name = "car")
    @Autowired()
   @Qualifier(value = "car")
    private  Car ca;



    public Car getCa() {
        return ca;
    }

    public void setCa(Car ca) {
        this.ca = ca;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

测试类没变  

直接看结果 

三月 07, 2018 10:52:22 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2280cdac: startup date [Wed Mar 07 22:52:22 CST 2018]; root of context hierarchy
三月 07, 2018 10:52:22 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [application-03.xml]
xxx
兰博基尼

 也是一样的 , 都能够 装配数据上去 

JDK和JRE的区别?
解析:JDK Java Delvelopment Kit Java 开发工具包
jar包 -----》各种类和接口
JRE:运行环境

静态打理  

了解一下静态代理满足的条件和他的弊端

1、需要知道核心类(被代理类)是哪一个类,并且有什么方法。 

2、非核心的代码需要重复写多次,显得代码的结构臃肿,形成代码冗余。

3、非核心类(代理类)需要实现核心类(被代理类)实现的接口,也就是他们需要实现共同的接口,但是以核心类实现的接口(被代理类)为准。

  现在写个东西,我们来认识一下 

  先定义一个接口  里面来一个方法 

  

public interface Subject {

    public void request();

}

   写个东西实现下

public class RealSubject implements Subject {
    public void request() {
        System.out.println("我是xxx");
    }
}

再写个  也实现以下方法 在写一个 上一个类的变量 new 之后 生成以下getset 属性   在他实现方法 内部 使用 另一个类的方法 

public class dali implements Subject {

    private  RealSubject realSubject=new RealSubject();


    public RealSubject getRealSubject() {
        return realSubject;
    }

    public void setRealSubject(RealSubject realSubject) {
        this.realSubject = realSubject;
    }

    public void request()
    {

        System.out.println("a");


        realSubject.request();


        System.out.println("b");
    }
}
 并且在他上面的 下面的 写个 a b 

测试
    @Test
    public void ad(){

        RealSubject re=new RealSubject();

        dali da=new dali();

        da.setRealSubject(re);

        da.request();


    }

a
我是xxx
b

以上就是控制台  执行 代理对象 

 

 








原文地址:https://www.cnblogs.com/LWLDD/p/8525937.html