bean注入优化,暂时只支持带一个参数

This commit is contained in:
aoshiguchen
2022-07-08 15:25:12 +08:00
parent 38b0c4ba20
commit 61b9e8f46e
6 changed files with 223 additions and 3 deletions
@@ -34,7 +34,7 @@ import java.lang.annotation.Target;
* @author: aoshiguchen
* @date: 2022/6/16
*/
@Target(ElementType.FIELD)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Autowired {
String value() default "";
@@ -123,7 +123,9 @@ public class SimpleBeanFactory extends AbstractBeanFactory {
}
Class<?> beanType = method.getReturnType();
if (method.getParameters().length > 0) {
throw new BeanException(String.format("Bean[type:%s name:%s] 注册失败,Class:%s method:%s 方法不能带参数!", beanType.getName(), beanName, type.getName(), method.getName()));
if (!(method.getParameters().length == 1 && method.getParameters()[0].isAnnotationPresent(Autowired.class))) {
throw new BeanException(String.format("Bean[type:%s name:%s] 注册失败,Class:%s method:%s 方法仅支持最多带一个参数,且参数必须带`Autowired`注解!", beanType.getName(), beanName, type.getName(), method.getName()));
}
}
BeanWrapper beanWrapper = addBean(beanType, beanName);
beanWrapper.setFactoryBean(factoryBean);
@@ -204,7 +206,24 @@ public class SimpleBeanFactory extends AbstractBeanFactory {
if (!factoryBean.hasInstance()) {
newInstance(factoryBean);
}
bean.setInstance(bean.getInstantiationMethod().invoke(factoryBean.getInstance()));
if (bean.getInstantiationMethod().getParameters().length == 0) {
bean.setInstance(bean.getInstantiationMethod().invoke(factoryBean.getInstance()));
} else {
// 暂时只处理一个参数的情况
Autowired autowired = bean.getInstantiationMethod().getParameters()[0].getAnnotation(Autowired.class);
String beanName = autowired.value();
Class<?> beanType = bean.getInstantiationMethod().getParameters()[0].getType();
Object arg = null;
if (BeanMatchMode.ByName == autowired.matchMode()) {
arg = getBean(beanName);
} else if(BeanMatchMode.ByType == autowired.matchMode()) {
arg = getBean(beanType);
} else {
arg = getBeanByTypeAndName(beanType, beanName);
}
bean.setInstance(bean.getInstantiationMethod().invoke(factoryBean.getInstance(), arg));
}
} else if (BeanInstantiationMode.FACTORY == bean.getInstantiationMode()) {
// 通过FactoryBean实例化
BeanWrapper factoryBean = bean.getFactoryBean();
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.bean.test3;
/**
*
* @author: aoshiguchen
* @date: 2022/7/8
*/
public class Dog {
private String name;
public Dog(String name) {
this.name = name;
System.out.println(String.format("%s出生了 %s", name, this.hashCode()));
}
public void call() {
System.out.println(String.format("%s叫:汪汪汪", this.name));
}
public String getName() {
return name;
}
}
@@ -0,0 +1,60 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.bean.test3;
import fun.asgc.neutrino.core.annotation.Autowired;
import fun.asgc.neutrino.core.annotation.Bean;
import fun.asgc.neutrino.core.annotation.NeutrinoApplication;
import fun.asgc.neutrino.core.context.ApplicationRunner;
import fun.asgc.neutrino.core.context.NeutrinoLauncher;
/**
*
* @author: aoshiguchen
* @date: 2022/7/8
*/
@NeutrinoApplication
public class Launcher implements ApplicationRunner {
@Autowired
private Person person;
@Override
public void run(String[] args) {
person.walkTheDog();
}
public static void main(String[] args) {
NeutrinoLauncher.run(Launcher.class, args).sync();
}
@Bean
public Dog dog() {
return new Dog("大黄");
}
@Bean
public Person person(@Autowired("dog") Dog dog) {
Person person = new Person("张三");
person.setPet(dog);
return person;
}
}
@@ -0,0 +1,46 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.bean.test3;
/**
*
* @author: aoshiguchen
* @date: 2022/7/8
*/
public class Person {
private String name;
private Dog pet;
public Person(String name) {
this.name = name;
System.out.println(String.format("%s出生了 %s", name, this.hashCode()));
}
public void setPet(Dog pet) {
this.pet = pet;
}
public void walkTheDog() {
System.out.println(String.format("%s在遛狗(%s %s)", this.name, pet.getName(), pet.hashCode()));
pet.call();
}
}
@@ -0,0 +1,52 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.core.bean.test3;
import fun.asgc.neutrino.core.aop.Invocation;
import fun.asgc.neutrino.core.aop.interceptor.Interceptor;
import lombok.extern.slf4j.Slf4j;
/**
*
* @author: aoshiguchen
* @date: 2022/6/24
*/
@Slf4j
public class TestInterceptor implements Interceptor {
public TestInterceptor() {
log.info("拦截器1实例化");
}
@Override
public void intercept(Invocation inv) {
try {
log.info("拦截器1 class:{} method:{} args:{} before", inv.getTargetClass().getName(), inv.getTargetMethod().getName(), inv.getArgs());
inv.invoke();
log.info("拦截器1 class:{} method:{} args:{} after", inv.getTargetClass().getName(), inv.getTargetMethod().getName(), inv.getArgs());
} catch (Exception e) {
log.info("拦截器1 class:{} method:{} args:{} error", inv.getTargetClass().getName(), inv.getTargetMethod().getName(), inv.getArgs());
e.printStackTrace();
}
}
}