欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring注解驱动开发

程序员文章站 2022-10-06 21:19:08
1 组件注册 1.1 @Configuration&@Bean注册Bean 1.1.1 传统的xml方式 Person.java package com.xuweiwei.spring.model; /** * @describe: * @author: 不为往事扰,余生只爱笑。 * @versio ......

1 组件注册

1.1 @configuration&@bean注册bean

1.1.1 传统的xml方式

  • person.java
Spring注解驱动开发
package com.xuweiwei.spring.model;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class person {
    private string name;
    private integer age;

    public person() {
    }

    public person(string name, integer age) {
        this.name = name;
        this.age = age;
    }


    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;
    }

    @override
    public string tostring() {
        return "person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
view code
  • applicationcontext.xml
<?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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    
    <bean id="person" class="com.xuweiwei.spring.model.person">
        <property name="name" value="许威威"/>
        <property name="age" value="27"/>
    </bean>

</beans>
  • 测试
Spring注解驱动开发
package com.xuweiwei.sping;

import com.xuweiwei.spring.model.person;
import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.test.context.contextconfiguration;
import org.springframework.test.context.junit4.springjunit4classrunner;

@runwith(springjunit4classrunner.class)
@contextconfiguration(locations = "classpath*:applicationcontext.xml")
public class springtest {
    @autowired
    private person person;


    @test
    public void test(){
        system.out.println(person);
    }

}
view code

1.1.2 注解方式

  • person.java
Spring注解驱动开发
package com.xuweiwei.spring.model;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class person {
    private string name;
    private integer age;

    public person() {
    }

    public person(string name, integer age) {
        this.name = name;
        this.age = age;
    }


    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;
    }

    @override
    public string tostring() {
        return "person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
view code
  • mainconfig.java
package com.xuweiwei.spring.config;

import com.xuweiwei.spring.model.person;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
//配置类==配置文件
//告诉spring这是一个配置类
@configuration //相当于applicationcontext.xml
public class mainconfig {

    /**
     * @bean 注解相当于bean标签
     *
     * @return
     */
    @bean
    public person person() {
        return new person("许威威", 27);
    }

}
  • 测试
Spring注解驱动开发
package com.xuweiwei.sping;

import com.xuweiwei.spring.config.mainconfig;
import com.xuweiwei.spring.model.person;
import org.junit.test;
import org.springframework.context.applicationcontext;
import org.springframework.context.annotation.annotationconfigapplicationcontext;

public class configurationspringtest {

    @test
    public void test() {
        applicationcontext context = new annotationconfigapplicationcontext(mainconfig.class);

        person person = context.getbean(person.class);
        system.out.println(person);

        string[] names = context.getbeandefinitionnames();
        for (string name : names) {
            system.out.println(name);
        }

    }

}
view code

1.2 @componentscan自动扫描

1.2.1 传统的xml方式

  • personcontroller.java
Spring注解驱动开发
package com.xuweiwei.spring.controller;

import org.springframework.stereotype.controller;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
@controller
public class personcontroller {
}
view code
  • personservice.java
Spring注解驱动开发
package com.xuweiwei.spring.service;

import org.springframework.stereotype.service;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
@service
public class personservice {
}
view code
  • persondao.java
Spring注解驱动开发
package com.xuweiwei.spring.dao;

import org.springframework.stereotype.repository;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
@repository
public class persondao {
}
view code
  • applicationcontext.xml
<?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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    <!--只要标注了@controller、@service、@component、@repository注解的都会自动注入到ioc容器中-->
    <context:component-scan base-package="com.xuweiwei.spring"></context:component-scan>
</beans>
  • 测试
Spring注解驱动开发
package com.xuweiwei.sping;

import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.context.applicationcontext;
import org.springframework.test.context.contextconfiguration;
import org.springframework.test.context.junit4.springjunit4classrunner;

@runwith(springjunit4classrunner.class)
@contextconfiguration(locations = "classpath*:applicationcontext.xml")
public class springtest {
    @autowired
    private applicationcontext context;

    @test
    public void test(){
        string[] names = context.getbeandefinitionnames();
        for (string name : names) {
            system.out.println(name);
        }
    }
}
view code

1.2.2 注解方式

  • person.java
Spring注解驱动开发
package com.xuweiwei.spring.model;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class person {
    private string name;
    private integer age;

    public person() {
    }

    public person(string name, integer age) {
        this.name = name;
        this.age = age;
    }


    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;
    }

    @override
    public string tostring() {
        return "person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
view code
  • persondao.java
Spring注解驱动开发
package com.xuweiwei.spring.dao;

import org.springframework.stereotype.repository;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
@repository
public class persondao {
}
view code
  • personservice.java
Spring注解驱动开发
package com.xuweiwei.spring.service;

import org.springframework.stereotype.service;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
@service
public class personservice {
}
view code
  • personcontroller.java
Spring注解驱动开发
package com.xuweiwei.spring.controller;

import org.springframework.stereotype.controller;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
@controller
public class personcontroller {
}
view code
  • mainconfig.java
package com.xuweiwei.spring.config;

import com.xuweiwei.spring.model.person;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.componentscan;
import org.springframework.context.annotation.configuration;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
//配置类==配置文件
//告诉spring这是一个配置类
@configuration //相当于applicationcontext.xml
@componentscan(value = "com.xuweiwei")
public class mainconfig {

    /**
     * @bean 注解相当于bean标签
     *
     * @return
     */
    @bean
    public person person() {
        return new person("许威威", 27);
    }

}
  • 测试
Spring注解驱动开发
package com.xuweiwei.sping;

import com.xuweiwei.spring.config.mainconfig;
import com.xuweiwei.spring.model.person;
import org.junit.test;
import org.springframework.context.applicationcontext;
import org.springframework.context.annotation.annotationconfigapplicationcontext;

public class configurationspringtest {

    @test
    public void test() {
        applicationcontext context = new annotationconfigapplicationcontext(mainconfig.class);

        person person = context.getbean(person.class);
        system.out.println(person);

        string[] names = context.getbeandefinitionnames();
        for (string name : names) {
            system.out.println(name);
        }

    }

}
view code

 

  • 如果是xml方式的只过滤controller注解标注的类,代码如下
<?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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    <!--只要标注了@controller、@service、@component、@repository注解的都会自动注入到ioc容器中,默认扫描全部-->
    <context:component-scan base-package="com.xuweiwei.spring" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.controller"/>
    </context:component-scan>
</beans>
  • 如果是注解方式只过滤controller注解标注的类,代码如下
package com.xuweiwei.spring.config;

import com.xuweiwei.spring.model.person;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.componentscan;
import org.springframework.context.annotation.configuration;
import org.springframework.context.annotation.filtertype;
import org.springframework.stereotype.controller;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
//配置类==配置文件
//告诉spring这是一个配置类
@configuration //相当于applicationcontext.xml
@componentscan(
        value = "com.xuweiwei",
        usedefaultfilters = false,
        includefilters = {@componentscan.filter(type = filtertype.annotation, classes = controller.class)}
)
public class mainconfig {

    /**
     * @return
     * @bean 注解相当于bean标签
     */
    @bean
    public person person() {
        return new person("许威威", 27);
    }

}

1.3 @scope注解

  • 设置组件的作用域。

 

  • 示例:
    • mainconfig.java  
package com.xuweiwei.spring.config;

import com.xuweiwei.spring.model.person;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.context.annotation.scope;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
//配置类==配置文件
//告诉spring这是一个配置类
@configuration //相当于applicationcontext.xml
public class mainconfig {
    /**
     * * @see configurablebeanfactory#scope_prototype
     * * @see configurablebeanfactory#scope_singleton
     * * @see org.springframework.web.context.webapplicationcontext#scope_request
     * * @see org.springframework.web.context.webapplicationcontext#scope_session
     * 默认情况下,是singleton
     *
     * singleton是单例
     * prototype是多例
     * request同一次请求创建一个实例
     * session同一个session创建一个实例
     * @return
     */
    @scope(value = "singleton")
    @bean
    public person person() {
        return new person("许威威", 27);
    }

}
    • 测试  
Spring注解驱动开发
package com.xuweiwei.sping;

import com.xuweiwei.spring.config.mainconfig;
import com.xuweiwei.spring.model.person;
import org.junit.test;
import org.springframework.context.applicationcontext;
import org.springframework.context.annotation.annotationconfigapplicationcontext;

public class configurationspringtest {

    @test
    public void test() {
        applicationcontext context = new annotationconfigapplicationcontext(mainconfig.class);

        person person1 = context.getbean(person.class);
        person person2 = context.getbean(person.class);
        system.out.println(person1 == person2);
    }

}
view code

 

  • 默认情况下,ioc容器启动会调用方法并创建对象放入到容器中,以后每次获取直接从容器中获取。
  • 如果scope的值是prototype,那么ioc容器启动的时候并不会调用方法,而是每次获取对象的时候创建对象并将对象放入到容器中。

1.4 @lazy注解

  • @lazy针对的是单实例的bean。

 

  • 示例:
package com.xuweiwei.spring.config;

import com.xuweiwei.spring.model.person;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.context.annotation.lazy;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
//配置类==配置文件
//告诉spring这是一个配置类
@configuration //相当于applicationcontext.xml
public class mainconfig {
    /**
     * 懒加载:容器启动的时候不创建对象,而是在第一次使用的时候,才创建对象
     * @return
     */
    @lazy
    @bean
    public person person() {
        system.out.println("给容器中注入bean^_^ ^_^ ^_^ ^_^");
        return new person("许威威", 27);
    }

}

1.5 @conditional注解

  • 根据条件给容器中注入bean。【springboot底层大量使用了这个注解】

 

  • 示例:
    • linuxcondition.java  
package com.xuweiwei.spring.condition;

import org.springframework.context.annotation.condition;
import org.springframework.context.annotation.conditioncontext;
import org.springframework.core.env.environment;
import org.springframework.core.type.annotatedtypemetadata;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class linuxcondition implements condition {
    @override
    public boolean matches(conditioncontext context, annotatedtypemetadata metadata) {
        environment environment = context.getenvironment();
        string property = environment.getproperty("os.name");

        if(property.contains("linux")){
            return true;
        }
        return false;
    }
}
    • windowscondition.java  
package com.xuweiwei.spring.condition;

import org.springframework.context.annotation.condition;
import org.springframework.context.annotation.conditioncontext;
import org.springframework.core.env.environment;
import org.springframework.core.type.annotatedtypemetadata;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class windowscondition implements condition {
    @override
    public boolean matches(conditioncontext context, annotatedtypemetadata metadata) {
        environment environment = context.getenvironment();
        string property = environment.getproperty("os.name");

        if(property.contains("windows")){
            return true;
        }
        return false;
    }
}
    • mainconfig2.java  
package com.xuweiwei.spring.config;

import com.xuweiwei.spring.condition.linuxcondition;
import com.xuweiwei.spring.condition.windowscondition;
import com.xuweiwei.spring.model.person;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.conditional;
import org.springframework.context.annotation.configuration;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
@configuration
public class mainconfig2 {

    @conditional(value = windowscondition.class)
    @bean("bill")
    public person person1(){
        return new person("比尔盖茨",60);
    }

    @conditional(value = linuxcondition.class)
    @bean("linus")
    public person person2(){
        return new person("林纳斯",50);
    }

}
    • 测试  
Spring注解驱动开发
package com.xuweiwei.sping;

import com.xuweiwei.spring.config.mainconfig2;
import org.junit.test;
import org.springframework.context.applicationcontext;
import org.springframework.context.annotation.annotationconfigapplicationcontext;

public class configurationspringtest {

    @test
    public void test() {
        applicationcontext context = new annotationconfigapplicationcontext(mainconfig2.class);
        string[] names = context.getbeandefinitionnames();
        for (string name : names) {
            system.out.println(name);
        }
    }

}
view code

 

  • 【注】:如果@conditional注解标注在类上,表示满足当前条件,这个类配置的所有bean注册才能生效。

1.6 @import注解

  • 给容器中快速导入一个组件。

 

  • ①@import(要导入容器的组件):容器就会自动注册这个组件。
  • 示例:
    • color.java  
Spring注解驱动开发
package com.xuweiwei.spring.model;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class color {
}
view code
    • person.java  
Spring注解驱动开发
package com.xuweiwei.spring.model;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class person {
    private string name;
    private integer age;

    public person() {
    }

    public person(string name, integer age) {
        this.name = name;
        this.age = age;
    }


    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;
    }

    @override
    public string tostring() {
        return "person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
view code
    • mainconfig.java  
package com.xuweiwei.spring.config;

import com.xuweiwei.spring.model.color;
import com.xuweiwei.spring.model.person;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.context.annotation.import;
import org.springframework.context.annotation.lazy;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
//配置类==配置文件
//告诉spring这是一个配置类
@import(value = color.class)
@configuration //相当于applicationcontext.xml
public class mainconfig {
    /**
     * 懒加载:容器启动的时候不创建对象,而是在第一次使用的时候,才创建对象
     * @return
     */
    @lazy
    @bean
    public person person() {
        system.out.println("给容器中注入bean^_^ ^_^ ^_^ ^_^");
        return new person("许威威", 27);
    }

}
    • 测试  
Spring注解驱动开发
package com.xuweiwei.sping;

import com.xuweiwei.spring.config.mainconfig;
import org.junit.test;
import org.springframework.context.applicationcontext;
import org.springframework.context.annotation.annotationconfigapplicationcontext;

public class configurationspringtest {

    @test
    public void test() {
        applicationcontext context = new annotationconfigapplicationcontext(mainconfig.class);
        string[] names = context.getbeandefinitionnames();
        for (string name : names) {
            system.out.println(name);
        }
    }

}
view code

 

  • ②使用importselector:返回需要导入组件的全类名数组。
  • 示例:
    • color.java  
Spring注解驱动开发
package com.xuweiwei.spring.model;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class color {
}
view code
    • red.java  
Spring注解驱动开发
package com.xuweiwei.spring.model;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class red {
}
view code
    • blue.java  
Spring注解驱动开发
package com.xuweiwei.spring.model;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class blue {
}
view code
    • person.java  
Spring注解驱动开发
package com.xuweiwei.spring.model;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class person {
    private string name;
    private integer age;

    public person() {
    }

    public person(string name, integer age) {
        this.name = name;
        this.age = age;
    }


    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;
    }

    @override
    public string tostring() {
        return "person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
view code
    • myimportselector.java
package com.xuweiwei.spring.condition;

import org.springframework.context.annotation.importselector;
import org.springframework.core.type.annotationmetadata;

/**
 * @describe: 自定义逻辑返回需要导入的组件
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class myimportselector implements importselector {
    /**
     * 返回值就是要导入到容器中的组件全类名
     * @param importingclassmetadata 当前标注@import注解的类的所有注解信息
     * @return
     */
    @override
    public string[] selectimports(annotationmetadata importingclassmetadata) {

        return new string[]{"com.xuweiwei.spring.model.blue","com.xuweiwei.spring.model.red"};
    }
}
    • mainconfig.java  
package com.xuweiwei.spring.config;

import com.xuweiwei.spring.condition.myimportselector;
import com.xuweiwei.spring.model.color;
import com.xuweiwei.spring.model.person;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.context.annotation.import;
import org.springframework.context.annotation.lazy;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
//配置类==配置文件
//告诉spring这是一个配置类
@import(value = {color.class, myimportselector.class})
@configuration //相当于applicationcontext.xml
public class mainconfig {
    /**
     * 懒加载:容器启动的时候不创建对象,而是在第一次使用的时候,才创建对象
     * @return
     */
    @lazy
    @bean
    public person person() {
        system.out.println("给容器中注入bean^_^ ^_^ ^_^ ^_^");
        return new person("许威威", 27);
    }

}
    • 测试  
Spring注解驱动开发
package com.xuweiwei.sping;

import com.xuweiwei.spring.config.mainconfig;
import org.junit.test;
import org.springframework.context.applicationcontext;
import org.springframework.context.annotation.annotationconfigapplicationcontext;

public class configurationspringtest {

    @test
    public void test() {
        applicationcontext context = new annotationconfigapplicationcontext(mainconfig.class);
        string[] names = context.getbeandefinitionnames();
        for (string name : names) {
            system.out.println(name);
        }
    }

}
view code

 

  • ③使用importbeandefinitionregistrar:可以手动注册指定名称的bean。
  • 示例:
    • blue.java  
Spring注解驱动开发
package com.xuweiwei.spring.model;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class blue {
}
view code
    • color.java  
Spring注解驱动开发
package com.xuweiwei.spring.model;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class color {
}
view code
    • person.java  
Spring注解驱动开发
package com.xuweiwei.spring.model;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class person {
    private string name;
    private integer age;

    public person() {
    }

    public person(string name, integer age) {
        this.name = name;
        this.age = age;
    }


    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;
    }

    @override
    public string tostring() {
        return "person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
view code
    • red.java  
Spring注解驱动开发
package com.xuweiwei.spring.model;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class red {
}
view code
    • white.java  
Spring注解驱动开发
package com.xuweiwei.spring.model;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class white {
}
view code
    • myimportbeandefinitionregistrar.java  
package com.xuweiwei.spring.condition;

import com.xuweiwei.spring.model.white;
import org.springframework.beans.factory.support.beandefinitionregistry;
import org.springframework.beans.factory.support.rootbeandefinition;
import org.springframework.context.annotation.importbeandefinitionregistrar;
import org.springframework.core.type.annotationmetadata;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class myimportbeandefinitionregistrar implements importbeandefinitionregistrar {
    /**
     * @param importingclassmetadata:当前标注@import注解的类的所有注解信息
     * @param registry:beandefinition注册类
     */
    @override
    public void registerbeandefinitions(annotationmetadata importingclassmetadata, beandefinitionregistry registry) {

        boolean flag = registry.containsbeandefinition("white");
        if (!flag) {
            registry.registerbeandefinition("white", new rootbeandefinition(white.class));
        }
    }
}
    • mainconfig.java
package com.xuweiwei.spring.config;

import com.xuweiwei.spring.condition.myimportbeandefinitionregistrar;
import com.xuweiwei.spring.condition.myimportselector;
import com.xuweiwei.spring.model.color;
import com.xuweiwei.spring.model.person;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.context.annotation.import;
import org.springframework.context.annotation.lazy;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
//配置类==配置文件
//告诉spring这是一个配置类
@import(value = {color.class, myimportselector.class, myimportbeandefinitionregistrar.class})
@configuration //相当于applicationcontext.xml
public class mainconfig {
    /**
     * 懒加载:容器启动的时候不创建对象,而是在第一次使用的时候,才创建对象
     * @return
     */
    @lazy
    @bean
    public person person() {
        system.out.println("给容器中注入bean^_^ ^_^ ^_^ ^_^");
        return new person("许威威", 27);
    }

}
    • 测试  
Spring注解驱动开发
package com.xuweiwei.sping;

import com.xuweiwei.spring.config.mainconfig;
import org.junit.test;
import org.springframework.context.applicationcontext;
import org.springframework.context.annotation.annotationconfigapplicationcontext;

public class configurationspringtest {

    @test
    public void test() {
        applicationcontext context = new annotationconfigapplicationcontext(mainconfig.class);
        string[] names = context.getbeandefinitionnames();
        for (string name : names) {
            system.out.println(name);
        }
    }

}
view code

1.7 使用factorybean注册bean

  • 示例:
    • colorfactorybean.java  
package com.xuweiwei.spring.model;

import org.springframework.beans.factory.factorybean;

/**
 * @describe: 创建spring提供的factorybean
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class colorfactorybean implements factorybean<color> {
    /**
     * 返回一个color对象,这个对象会添加到容器中
     * @return
     * @throws exception
     */
    @override
    public color getobject() throws exception {
        return new color();
    }

    @override
    public class<?> getobjecttype() {
        return color.class;
    }

    @override
    public boolean issingleton() {
        return true;
    }
}
    • mainconfig.java  
package com.xuweiwei.spring.config;

import com.xuweiwei.spring.model.colorfactorybean;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
//配置类==配置文件
//告诉spring这是一个配置类
@configuration //相当于applicationcontext.xml
public class mainconfig {

    @bean
    public colorfactorybean colorfactorybean(){
        return new colorfactorybean();
    }


}
    • 测试  
Spring注解驱动开发
package com.xuweiwei.sping;

import com.xuweiwei.spring.config.mainconfig;
import org.junit.test;
import org.springframework.context.applicationcontext;
import org.springframework.context.annotation.annotationconfigapplicationcontext;

public class configurationspringtest {

    @test
    public void test() {
        applicationcontext context = new annotationconfigapplicationcontext(mainconfig.class);
        string[] names = context.getbeandefinitionnames();
        for (string name : names) {
            system.out.println(name);
        }
    }

}
view code

 

2 生命周期

2.1 @bean指定初始化方法和销毁方法

  •  示例:
    • car.java  
package com.xuweiwei.spring.model;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class car {

    public car() {
        system.out.println("构造方法被调用了o(∩_∩)o哈哈~");
    }

    public void init() {
        system.out.println("初始化了^_^");
    }

    public void destroy() {
        system.out.println("销毁了o(╥﹏╥)o");
    }
}
    • mainconfig3.java  
package com.xuweiwei.spring.config;

import com.xuweiwei.spring.model.car;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
@configuration
public class mainconfig3 {

    @bean(initmethod = "init",destroymethod = "destroy")
    public car car(){
        return new car();
    }

}
    • 测试  
Spring注解驱动开发
package com.xuweiwei.sping;

import com.xuweiwei.spring.config.mainconfig3;
import org.junit.test;
import org.springframework.context.applicationcontext;
import org.springframework.context.annotation.annotationconfigapplicationcontext;

public class configurationspringtest {

    @test
    public void test() {
        applicationcontext context = new annotationconfigapplicationcontext(mainconfig3.class);
        string[] names = context.getbeandefinitionnames();
        for (string name : names) {
            system.out.println(name);
        }

        ((annotationconfigapplicationcontext) context).close();
    }

}
view code

 

 

 

 

3 属性赋值