如何在抽象类中注⼊bean
抽象类是⽆法实例化的,因此⽆法使⽤@Service等这种注解直接将抽象类交给ioc容器管理,但是项⽬中往往需要有很多⼦类公⽤抽象⽗类的模板⽅法,那么怎么实现呢?
错误演⽰
1、抽象类
@Component
public abstract class BaseService {
@Autowired
Dao dao;
}
2、⼦类
@Component
public class MyService extends BaseService{
public void print(){
//运⾏时为null
System.out.String());
}
}
在我们实例化⼦类对象的时候,抽象⽗类不能实例化,因为spring注⼊的是实例对象,⽽不是类,所以spring不会将dao⾃动装配注⼊到⼀个实例中。
解决⽅法
⼀、使⽤ApplicationContextAware
1、⼯程图
jar包只需要引⼊spring-context即可。
2、ApplicationContextUtil
package spring.chapter1.utils;
import org.springframework.beans.BeansException;
import t.ApplicationContext;
import t.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextUtil.applicationContext = applicationContext;
}
public static Object getObject(String id) {
Object object = null;
object = Bean(id);
return object;
}
public static ApplicationContext getSpringContext() {
return applicationContext;
}
}
3、抽象类
package spring.chapter1.service;
import spring.chapter1.dao.Dao;
import spring.chapter1.utils.ApplicationContextUtil;
public abstract class BaseService {
Dao dao;
public BaseService() {
this.dao = (Dao) Object("dao");
}
}
4、⼦类
package spring.chapter1.service;
import t.annotation.DependsOn;
import org.springframework.stereotype.Component;
@Component
spring ioc注解/**
* 因为BaseService中构造器需要使⽤applicationContextUtil这个bean,所以需要加@DependsOn这个注解。 *注解作⽤:1、保证applicationContextUtil总是在MyService之前实例化
*      2、保证applicationContextUtil总是在MyService销毁之后销毁
*/
@DependsOn("applicationContextUtil")
public class MyService extends BaseService{
public MyService() {
super();
}
public void print(){
dao.process();
}
}
5、Dao(⼀个demo组件,模拟调⽤某个bean的⽅法⽽已)
package spring.chapter1.dao;
import org.springframework.stereotype.Component;
@Component
public class Dao {
public void process() {
      System.out.println("抽象⽗类中成功注⼊dao");
}
}
6、bean配置类(定义bean扫描策略)
package fig;
import t.annotation.ComponentScan;
import t.annotation.Configuration;
@Configuration
@ComponentScan(value = "spring.chapter1")
public class BeanConfig {
}
7、测试类
package spring.chapter1.main;
import org.junit.Test;
import t.annotation.AnnotationConfigApplicationContext;
import fig.BeanConfig;
import spring.chapter1.service.MyService;
public class SpringMain {
@Test
public void test() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeanConfig.class);        MyService myService = (MyService) Bean("myService");
myService.print();
}
}
运⾏结果:
⼆、⼦类bean为⽗类注⼊bean
1、抽象类
public abstract class BaseService {
Dao dao;
}
2、⼦类
@Component
public class myService extends BaseService{
//Autowired修饰⽅法时,根据⽅法参数类型判断实例化哪个类    @Autowired
public void printDao(Dao dao){
super.dao = dao;//⽗类属性注⼊
}
public void print(){
System.out.String());
}
}
通过这种⽅式,抽象类就可以获取到bean,并进⾏使⽤了。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。