spring容器启动之我见(四、获取容器和获取容器中的bean)

来源:互联网 发布:发起淘宝众筹的条件 编辑:程序博客网 时间:2024/05/21 17:01

1.我们经常会发现在我们的service中有注解而在dao上没有注解

看图




因为我们在spring容器初始化bean的时候,是把service当做一个bean ,而dao并不是一个bean,这是个人理解,如果有错误,欢迎大家指出。

2. 那我们总结一下那些算是spring 的bean吧。

直接看下面   在类的上面 添加了如下四个注解的我们都看作是spring的bean.

@Component、@Repository @Service、@Controller

  看字面含义,很容易却别出其中三个:

  @Controller 控制层,就是我们的action层

  @Service 业务逻辑层,就是我们的service或者manager层

  @Repository 持久层,就是我们常说的DAO层

  而@Component (字面意思就是组件),它在你确定不了事哪一个层的时候使用。

  其实,这四个注解的效果都是一样的,Spring都会把它们当做需要注入的Bean加载在上下文中;

  但是在项目中,却建议你严格按照除Componen的其余三个注解的含义使用在项目中。这对分层结构的web架构很有好处!!

  示例:

  1. 控制层

  @Controller // 注释为controller

  @RequestMapping("/login")

  public class LoginAction {

  @Autowired

  @Qualifier("userService") //注释指定注入 Bean

  private IUserService userService;

  。。。。。。 其他略 。。。。。。

  }

  2. 业务逻辑层

  @Service("userService")

  public class UserServiceImpl implements IUserService {

  @Autowired

  @Qualifier("userDao")

  private IUserDao userDao;

  。。。。。。 其他略 。。。。。。

  }

  3. 持久层

  @Repository("userDao")

  public class UserDaoImpl implements IUserDao {

  private static Logger logger = LoggerFactory.getLogger(UserDaoImpl.class);

  private DataSource dataSource;

  private JdbcTemplate template;

  @Autowired

  public UserDaoImpl(DataSource dataSource){

  this.dataSource= dataSource;

  template = new JdbcTemplate(this.dataSource);

  }


3.问题来了。像我们的springmvc框架。在持久层的 Dao它是没有实现类的。因为我们写的dao直接继承CrudRepository<xx, xx> 

这时候我们写的dao算不算bean呢。。本人实验过

import org.springframework.beans.factory.BeanFactory;

在controller中测试的。

@Autowired
private BeanFactory beanfactory;

我这里的。BmWorkDAO 就是继承CrudRepository<xx, xx>  没有实现类,因为继承CrudRepository<xx, xx>也是不需要实现类的。稍微啰嗦一点。

String ss = "org.szgzw.ent.compare.bmwork.BmWorkDAO";
BmWorkDAO bb = (BmWorkDAO)beanfactory.getBean(Class.forName(ss));

System.out.println(bb.getClass().getName());

反正没有报错,具体这里的BmWorkDAO 算不算bean,本人也不是很懂,既然不报错,本人认为还是算是spring容器的bean。


4.我们对第3点做一点补充说明吧。

我们看到我们在controller中注入了。

@Autowired
private BeanFactory beanfactory;

我们直接是注入bean工厂然后去获取到bean .中方式使用场景是,容器处于启动中即tomcat处于启动中。注意这种获取bean的方式吧。


5.上面3和4的方法是通过注入BeanFactory然后获取spring容器中的bean.下面用另外一种方式来获取bean.首先我们是获取spring容器的上下文

既然使用了Spring,那就可以通过注解形式,在controller中获取到servletContext;包:import javax.servlet.ServletContext;
@Autowired 
private ServletContext servletContext;


搜索我们直接来看代码:

ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);    //获取spring容器

BmWorkService bb = (BmWorkService)ac1.getBean("bmworkService");               //通过spring容器获取bean
System.out.println(bb.getClass().getName());


6.我们接着看下面方式获取bean

由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出 WebApplicationContext 对象: 

这里我们同样注入

@Autowired 
private ServletContext servletContext;

利用如下代码就可以获取spring容器

WebApplicationContext  webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

BmWorkService bb = (BmWorkService)webApplicationContext .getBean("bmworkService");               
System.out.println(bb.getClass().getName());




1 0