Springboot自动装配实现过程代码实例
程序员文章站
2022-06-06 16:06:19
创建一个简单的项目:
创建一个简单的项目:
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <parent> <artifactid>spring-boot-starter-parent</artifactid> <groupid>org.springframework.boot</groupid> <version>2.1.12.release</version> </parent> <groupid>com.xiazhi</groupid> <artifactid>demo</artifactid> <version>1.0-snapshot</version> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> </dependency> </dependencies> </project>
首先创建自定义注解:
package com.xiazhi.demo.annotation; import java.lang.annotation.elementtype; import java.lang.annotation.retention; import java.lang.annotation.retentionpolicy; import java.lang.annotation.target; /** * mycomponent 作用于类上,表示这是一个组件,于component,service注解作用相同 * @author zhaoshuai */ @target(elementtype.type) @retention(retentionpolicy.runtime) public @interface mycomponent { }
package com.xiazhi.demo.annotation; import java.lang.annotation.elementtype; import java.lang.annotation.retention; import java.lang.annotation.retentionpolicy; import java.lang.annotation.target; /** * 作用于字段上,自动装配的注解,与autowired注解作用相同 * @author zhaoshuai */ @target(elementtype.field) @retention(retentionpolicy.runtime) public @interface reference { }
然后写配置类:
package com.xiazhi.demo.config; import com.xiazhi.demo.annotation.mycomponent; import org.springframework.beans.factory.support.beandefinitionregistry; import org.springframework.context.resourceloaderaware; import org.springframework.context.annotation.classpathbeandefinitionscanner; import org.springframework.context.annotation.importbeandefinitionregistrar; import org.springframework.core.io.resourceloader; import org.springframework.core.type.annotationmetadata; import org.springframework.core.type.filter.annotationtypefilter; /** * @author zhaoshuai * @company lihfinance.com * @date create in 2020/3/21 **/ public class componentautoconfiguration implements importbeandefinitionregistrar, resourceloaderaware { private resourceloader resourceloader; @override public void registerbeandefinitions(annotationmetadata annotationmetadata, beandefinitionregistry beandefinitionregistry) { string classname = annotationmetadata.getclassname(); string basepackages = classname.substring(0, classname.lastindexof(".")); classpathbeandefinitionscanner scanner = new classpathbeandefinitionscanner(beandefinitionregistry, false); scanner.addincludefilter(new annotationtypefilter(mycomponent.class)); scanner.scan(basepackages); scanner.setresourceloader(resourceloader); } @override public void setresourceloader(resourceloader resourceloader) { this.resourceloader = resourceloader; } }
上面是配置扫描指定包下被mycomponent注解标注的类并注册为spring的bean,bean注册成功后,下面就是属性的注入了
package com.xiazhi.demo.config; import com.xiazhi.demo.annotation.mycomponent; import com.xiazhi.demo.annotation.reference; import org.springframework.beans.beansexception; import org.springframework.beans.factory.config.beanpostprocessor; import org.springframework.boot.springbootconfiguration; import org.springframework.context.applicationcontext; import org.springframework.context.applicationcontextaware; import org.springframework.context.annotation.bean; import org.springframework.util.reflectionutils; import java.lang.reflect.field; /** * @author zhaoshuai * @company lihfinance.com * @date create in 2020/3/21 **/ @springbootconfiguration public class configuration implements applicationcontextaware { private applicationcontext applicationcontext; @bean public beanpostprocessor beanpostprocessor() { return new beanpostprocessor() { /** * @company lihfinance.com * @author create by zhaoshuai in 2020/3/21 * 在bean注册前会被调用 * @param [bean, beanname] * @return java.lang.object **/ @override public object postprocessbeforeinitialization(object bean, string beanname) throws beansexception { return bean; } /** * @company lihfinance.com * @author create by zhaoshuai in 2020/3/21 * 在bean注册后会被加载,本次在bean注册成功后注入属性值 * @param [bean, beanname] * @return java.lang.object **/ @override public object postprocessafterinitialization(object bean, string beanname) throws beansexception { class<?> clazz = bean.getclass(); if (!clazz.isannotationpresent(mycomponent.class)) { return bean; } field[] fields = clazz.getdeclaredfields(); for (field field : fields) { if (!field.isannotationpresent(reference.class)) { continue; } class<?> type = field.gettype(); object obj = applicationcontext.getbean(type); reflectionutils.makeaccessible(field); reflectionutils.setfield(field, bean, obj); } return bean; } }; } @override public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception { this.applicationcontext = applicationcontext; } }
下面开始使用注解来看看效果:
package com.xiazhi.demo.service; import com.xiazhi.demo.annotation.mycomponent; import javax.annotation.postconstruct; /** * @author zhaoshuai * @company lihfinance.com * @date create in 2020/3/21 **/ @mycomponent public class myservice { @postconstruct public void init() { system.out.println("hello world"); } public void test() { system.out.println("测试案例"); } }
package com.xiazhi.demo.service; import com.xiazhi.demo.annotation.mycomponent; import com.xiazhi.demo.annotation.reference; /** * @author zhaoshuai * @company lihfinance.com * @date create in 2020/3/21 **/ @mycomponent public class myconsumer { @reference private myservice myservice; public void aaa() { myservice.test(); } }
启动类要引入配置文件:
import注解引入配置文件。
编写测试类测试:
@springboottest(classes = applicationrun.class) @runwith(springrunner.class) public class testdemo { @autowired public myconsumer myconsumer; @test public void fun1() { myconsumer.aaa(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 明星电商直播带货可行吗?