提交 9ace56e9 编写于 作者: 希川's avatar 希川

<fix>: 通过注解,注入属性信息失败的BUG

    a. 补充单元测试
上级 2bb37d5d
package cn.noexception.container.context.annotation;
import cn.hutool.core.util.StrUtil;
import cn.noexception.container.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import cn.noexception.container.factory.config.BeanDefinition;
import cn.noexception.container.factory.stereotype.Cube;
import cn.noexception.container.factory.support.BeanDefinitionRegistry;
......@@ -9,7 +10,7 @@ import java.util.Set;
/**
* ClassPathBeanDefinitionScanner
*
* <p>
* 扫描包处理
*
* @author 吕滔
......@@ -34,6 +35,9 @@ public class ClassPathBeanDefinitionScanner extends ClassPathScanningCandidateCo
registry.registerBeanDefinition(determineBeanName(beanDefinition), beanDefinition);
}
}
// 注册处理注解的 BeanPostProcessor (@Inject , @InputValue)
registry.registerBeanDefinition("cn.noexception.container.context.annotation.internalAutowiredAnnotationProcessor", new BeanDefinition(AutowiredAnnotationBeanPostProcessor.class));
}
private String determineBeanName(BeanDefinition beanDefinition) {
......
......@@ -13,7 +13,7 @@ import cn.noexception.container.context.support.ClassPathXmlApplicationContext;
import cn.noexception.test.bean.IUserService;
import cn.noexception.test.bean.UserServiceBeforeAdvice;
import cn.noexception.test.bean.UserServiceInterceptor;
import cn.noexception.test.bean.impl.UserService;
import cn.noexception.test.bean.UserService;
import org.aopalliance.intercept.MethodInterceptor;
import org.junit.Before;
import org.junit.Test;
......@@ -117,6 +117,13 @@ public class AopTest {
System.out.println("测试结果:" + userService.queryUserInfo());
}
@Test
public void test_inject() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-inject.xml");
IUserService userService = applicationContext.getBean("userService", IUserService.class);
System.out.println("测试结果:" + userService.queryUserInfo());
}
}
......
......@@ -54,7 +54,7 @@ public class ApiRunner {
@Test
public void test_dynamic(){
// 目标对象
IUserService userService = new cn.noexception.test.bean.impl.UserService();
IUserService userService = new UserService();
// 组装代理信息
AdvisedSupport advisedSupport = new AdvisedSupport();
advisedSupport.setTargetSource(new TargetSource(userService));
......
package cn.noexception.test.bean;
import cn.noexception.container.factory.stereotype.Cube;
import java.util.HashMap;
import java.util.Map;
/**
* UserDao
*
* @author 吕滔
* @Date 2021/11/9 13:37
*/
@Cube
public class UserDao {
private static Map<String, String> hashMap = new HashMap<>();
static {
hashMap.put("10001", "山鸡,香港, 旺角");
hashMap.put("10002", "陈浩南,香港, 尖沙咀");
hashMap.put("10003", "大头菜,香港, 旺角");
hashMap.put("10004", "肥嗨,香港, 旺角");
}
public String queryUserName(String uId) {
return hashMap.get(uId);
}
}
package cn.noexception.test.bean;
public class UserService {
private String uId;
private String company;
private String location;
private IUserDao userDao;
import cn.noexception.container.factory.annotation.Inject;
import cn.noexception.container.factory.annotation.InputValue;
import cn.noexception.container.factory.stereotype.Cube;
public String queryUserInfo() {
return userDao.queryUserName(uId) + "," + company + "," + location;
}
import java.util.Random;
public String getuId() {
return uId;
}
/**
* UserService
*
* @author 吕滔
* @Date 2021/11/3 17:01
*/
@Cube("userService")
public class UserService implements IUserService {
public void setuId(String uId) {
this.uId = uId;
}
@InputValue("${token}")
private String token;
public String getCompany() {
return company;
}
@Inject
private UserDao userDao;
public void setCompany(String company) {
this.company = company;
@Override
public String queryUserInfo() {
try {
Thread.sleep(new Random(1).nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
}
return userDao.queryUserName("10001") + ", " + token;
}
public String getLocation() {
return location;
@Override
public String register(String userName) {
try {
Thread.sleep(new Random(1).nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
}
return "注册用户:" + userName + " success! ";
}
public void setLocation(String location) {
this.location = location;
@Override
public String toString() {
return "UserService#token = { " + token + " }";
}
public IUserDao getUserDao() {
return userDao;
public String getToken() {
return token;
}
public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
public void setToken(String token) {
this.token = token;
}
}
package cn.noexception.test.bean.impl;
import cn.noexception.container.factory.stereotype.Cube;
import cn.noexception.test.bean.IUserService;
import java.util.Random;
/**
* UserService
*
* @author 吕滔
* @Date 2021/11/3 17:01
*/
@Cube("userService")
public class UserService implements IUserService {
private String token;
@Override
public String queryUserInfo() {
try {
Thread.sleep(new Random(1).nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
}
return "希川,100000,广州";
}
@Override
public String register(String userName) {
try {
Thread.sleep(new Random(1).nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
}
return "注册用户:" + userName + " success! ";
}
@Override
public String toString() {
return "UserService#token = { " + token + " }";
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
package cn.noexception.test.event;
import cn.noexception.container.ApplicationListener;
import cn.noexception.container.context.event.ContextRefreshEvent;
/**
* ContextRefreshListener
*
* @author 吕滔
* @Date 2021/11/10 16:56
*/
public class ContextRefreshListener implements ApplicationListener<ContextRefreshEvent> {
@Override
public void onApplicationEvent(ContextRefreshEvent event) {
System.out.println("容器初始化完成");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="userService" class="cn.noexception.test.bean.impl.UserService"/>
<bean id="userService" class="cn.noexception.test.bean.UserService"/>
<bean class="cn.noexception.container.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
......
<?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.xsd
http://www.springframework.org/schema/context">
<bean class="cn.noexception.container.factory.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:token.properties"/>
</bean>
<bean class="cn.noexception.test.event.ContextRefreshListener"/>
<context:component-scan base-package="cn.noexception.test.bean"/>
</beans>
\ No newline at end of file
......@@ -10,7 +10,7 @@
<property name="location" value="classpath:token.properties"/>
</bean>
<bean id="userService" class="cn.noexception.test.bean.impl.UserService">
<bean id="userService" class="cn.noexception.test.bean.UserService">
<property name="token" value="${token}"/>
</bean>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册