使用classPath自动扫描装配(貌似PersonDaoBean没有装配进来)
来源:互联网 发布:电信80端口什么 编辑:程序博客网 时间:2024/04/30 00:28
cn.dao
- <pre name="code" class="java">package cn.dao;
- public interface PersonDaoInterface {
- public abstract void add();
- }
cn.dao.imp
package cn.dao.imp;import org.springframework.stereotype.Service;import cn.dao.PersonDaoInterface;@Servicepublic class PersonDao implements PersonDaoInterface {public void add(){System.out.println("执行PersonDao.add()");}}
cn.service
- package cn.service;
- public interface PersonService {
- public abstract void save();
- }
cn.service.imp
package cn.service.imp;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Service;import cn.dao.imp.PersonDao;import cn.service.PersonService;@Servicepublic class PersonServiceBean implements PersonService {private String name;private PersonDao personDao;public String getName() {return name;}public void setName(String name) {this.name = name;}public PersonDao getPersonDao() {return personDao;}public void setPersonDao(PersonDao personDao) {this.personDao = personDao;}public void save(){personDao.add();}}
juintest
package junit.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.service.imp.PersonServiceBean;public class SpringTest {@Test public void instanceSpring(){AbstractApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"});//这里不一样,因为我们没有bean文件,所以把这里的PersonServiceBean改为personServiceBeanPersonServiceBean personServiceBean=(PersonServiceBean) ctx.getBean("personServiceBean");//这个时候PersonServiceBean已经注入,但是貌似PersonDaoBean没有注入。System.out.println(personServiceBean);personServiceBean.save();ctx.close();}}
<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="cn"></context:component-scan></beans>
- 使用classPath自动扫描装配(貌似PersonDaoBean没有装配进来)
- 使用classPath自动扫描装配(使用PostConstruct注解)
- 使用classPath自动扫描装配(使用PreDestroy注解)
- 使用classPath自动扫描装配(修改名称)
- 使用classPath自动扫描装配(使用scope注解修改该为prototype(原型))
- Spring自动扫描装配bean
- 注解,扫描范围,自动装配
- spring 使用注解自动装配和自动扫描机制
- Spring装配Bean之组件扫描和自动装配
- Spring装配Bean之组件扫描和自动装配
- Spring装配Bean之组件扫描和自动装配
- Spring自动扫描与自动装配
- Spring常用注解,自动扫描装配Bean
- Spring常用注解,自动扫描装配Bean
- Spring常用注解,自动扫描装配Bean
- Spring自动扫描和装配bean
- Spring常用注解,自动扫描装配Bean
- Spring常用注解,自动扫描装配Bean
- 利用ServletContext读取系统资源
- Blazeds复杂数据类型传输及类映射
- 一篇博客读后的几点感受
- Android测试教程(16):monkeyrunner简介
- Android OpenGL ES 开发教程(16):Viewing和Modeling(MODELVIEW) 变换
- 使用classPath自动扫描装配(貌似PersonDaoBean没有装配进来)
- MySQL无法启动、无法停止解决方法
- 数据库主键外键设计原则
- c++字节对齐
- Kinect Accelerator 孵化项目:创业公司初露锋芒
- 华为上机题总结(1)
- pacman命令
- 怎样学习2.0(也就是怎样实现自己的梦想)?希望是终结版
- 机器视觉:特征提取