基于之前的添加pom文件
<dependency>
<groupId>org.reflections
</groupId>
<artifactId>reflections
</artifactId>
<version>0.9.12
</version>
</dependency>
<dependency>
<groupId>org.apache.commons
</groupId>
<artifactId>commons-lang3
</artifactId>
<version>3.10
</version>
</dependency>
配置文件
basePackage=com.liu
注解
@Target({ElementType
.CONSTRUCTOR
, ElementType
.METHOD
, ElementType
.PARAMETER
, ElementType
.FIELD
, ElementType
.ANNOTATION_TYPE
})
@Retention(RetentionPolicy
.RUNTIME
)
@Documented
public @
interface Autowired {
boolean required() default true;
}
@Documented
@Target(ElementType
.TYPE
)
@Retention(RetentionPolicy
.RUNTIME
)
public @
interface Service {
String
value() default "";
}
@Target({ElementType
.TYPE
})
@Retention(RetentionPolicy
.RUNTIME
)
@Documented
public @
interface Transactional {
String
value() default "transactionManager";
}
在dao service TransactionManager ProxyFactory修改使用以上注解
dao
@Service("accountDao")
public class JdbcAccountDaoImpl implements AccountDao {
@Autowired
private ConnectionUtils connectionUtils
;
service
@Service("transferService")
@Transactional
public class TransferServiceImpl implements TransferService {
@Autowired
private AccountDao accountDao
;
TransactionManager
@Service("transactionManager")
public class TransactionManager {
@Autowired
private ConnectionUtils connectionUtils
;
ProxyFactory
@Service("proxyFactory")
public class ProxyFactory {
@Autowired
private TransactionManager transactionManager
;
public void setTransactionManager(TransactionManager transactionManager
) {
this.transactionManager
= transactionManager
;
}
新增一个工具类
public class PropertiesUtils {
private static Properties props
= new Properties();
public static Properties
getProperties(String path
) {
InputStream in
= PropertiesUtils
.class.getClassLoader().getResourceAsStream(path
);
try {
props
.load(in
);
} catch (IOException e
) {
e
.printStackTrace();
}
return props
;
}
}
修改 BeanFactory原来改名BeanFactoryOld不用,新建BeanFactory
public class BeanFactory {
private static Map
<String, Object> map
= new ConcurrentHashMap<>();
private static String basePackage
= "com.liu";
private static String basePackageKey
= "basePackage";
static {
try {
doInitializationBean();
doAutowired();
doTransactional();
} catch (InstantiationException e
) {
e
.printStackTrace();
} catch (IllegalAccessException e
) {
e
.printStackTrace();
}
}
private static void doInitializationBean() throws InstantiationException
, IllegalAccessException
{
Properties properties
= PropertiesUtils
.getProperties("base.properties");
String property
= properties
.getProperty(basePackageKey
);
if (StringUtils
.isBlank(property
)) {
property
= basePackage
;
}
Reflections reflections
= new Reflections(property
);
Set
<Class
<?>> classSet
= reflections
.getTypesAnnotatedWith(Service
.class);
for (Class
<?> clazz
: classSet
) {
Object newInstance
= clazz
.newInstance();
Service annotation
= clazz
.getAnnotation(Service
.class);
if(StringUtils
.isBlank(annotation
.value())) {
Class
<?>[] interfaces
= clazz
.getInterfaces();
if (interfaces
.length
> 1) {
throw new RuntimeException("该有多个父接口,并没有指定名称,请使用Service注解指定名称");
}
if (interfaces
.length
== 0) {
String simpleName
= clazz
.getSimpleName();
String key
= StringUtils
.uncapitalize(simpleName
);
map
.put(key
,newInstance
);
} else {
String simpleName
= interfaces
[0].getSimpleName();
String key
= StringUtils
.uncapitalize(simpleName
);
map
.put(key
,newInstance
);
}
}else {
String id
= annotation
.value();
String key
= StringUtils
.uncapitalize(id
);
map
.put(key
,newInstance
);
}
}
}
private static void doAutowired() {
try {
for(Map
.Entry
<String,Object> entry
:map
.entrySet()){
String objKey
= entry
.getKey();
Object value
= entry
.getValue();
Class
clazz =value
.getClass();
Field
[] fields
= clazz
.getDeclaredFields();
for (Field field
: fields
) {
if(field
.isAnnotationPresent(Autowired
.class)&& field
.getAnnotation(Autowired
.class).required()){
String simpleName
= field
.getType().getSimpleName();
String key
= StringUtils
.uncapitalize(simpleName
);
Object o
= map
.get(key
);
if(o
==null
){
throw new RuntimeException(simpleName
+"依赖的对象不存在 是null");
}
Method
[] methods
= clazz
.getMethods();
for (Method method
: methods
) {
if(method
.getName().equalsIgnoreCase("set"+simpleName
)){
method
.invoke(value
, o
);
}
}
}
}
map
.put(objKey
, value
);
}
} catch (IllegalAccessException e
) {
e
.printStackTrace();
} catch (InvocationTargetException e
) {
e
.printStackTrace();
}
}
private static void doTransactional() {
for(Map
.Entry
<String,Object> entry
:map
.entrySet()) {
String key
= entry
.getKey();
Object value
= entry
.getValue();
Class
clazz = value
.getClass();
if(clazz
.isAnnotationPresent(Transactional
.class)){
ProxyFactory proxyFactory
= BeanFactory
.getBean("proxyFactory", ProxyFactory
.class);
Object proxyObj
=null
;
Class
[] interfaces
= clazz
.getInterfaces();
if(interfaces
!=null
&&interfaces
.length
>0){
proxyObj
= proxyFactory
.getJdkProxy(value
);
}else{
proxyObj
= proxyFactory
.getCglibProxy(value
);
}
map
.put(key
, proxyObj
);
}
}
}
public static Object
getBean(String id
) {
return map
.get(id
);
}
public static <T> T
getBean(String id
,Class
<T> clazz
) {
return (T
)map
.get(id
);
}
}
修改servlet
public class TransferServlet extends HttpServlet {
private TransferService transferService
= BeanFactory
.getBean("transferService",TransferService
.class) ;
修改完成