使用classPath自动扫描装配(貌似PersonDaoBean没有装配进来)

cn.dao

[java]  view plain copy
  1. <pre name="code" class="java">package cn.dao;  
  2.   
  3. public interface PersonDaoInterface {  
  4.   
  5.     public abstract void add();  
  6.   
  7. }  


 

cn.dao.imp

package cn.dao.imp;

import org.springframework.stereotype.Service;

import cn.dao.PersonDaoInterface;
@Service
public class PersonDao implements PersonDaoInterface {
	public void add()
	{
		System.out.println("执行PersonDao.add()");
	}

}


cn.service

[java]  view plain copy
  1. package cn.service;  
  2.   
  3. public interface PersonService {  
  4.   
  5.     public abstract void save();  
  6.   
  7. }  

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;
@Service
public 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改为personServiceBean
		PersonServiceBean personServiceBean=(PersonServiceBean) ctx.getBean("personServiceBean");
		
		//这个时候PersonServiceBean已经注入,但是貌似PersonDaoBean没有注入。
		System.out.println(personServiceBean);
		personServiceBean.save();
		ctx.close();
	}
}



bean.xml


<?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>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可能出现这种情况的原因有很多,以下是一些可能的解决方案: 1. 确保在配置文件中正确配置了需要自动装配的对象,例如: ``` @Configuration @PropertySource("classpath:application.properties") public class AppConfig { @Autowired private Environment env; @Bean public MyConfig myConfig() { MyConfig config = new MyConfig(); config.setUrl(env.getProperty("my.url")); config.setUsername(env.getProperty("my.username")); config.setPassword(env.getProperty("my.password")); return config; } } ``` 2. 确保配置文件的路径和文件名是正确的,例如: ``` @PropertySource("classpath:application.properties") ``` 这里的classpath表示classpath下的根目录,如果配置文件在子目录下,需要在路径中添加相应的目录。 3. 确保项目的classpath正确配置,例如: 在IDEA中,可以在项目的“Run/Debug Configurations”中查看项目的classpath,确保配置文件在classpath下。 4. 确保需要自动装配的对象类上使用了@Component、@Configuration、@Service等注解,例如: ``` @Component public class MyConfig { private String url; private String username; private String password; // getter and setter methods } ``` 这里使用了@Component注解,表示这是一个Spring组件,可以被自动扫描装配。 5. 确保使用了正确的@Autowired注解,例如: ``` @Autowired private MyConfig myConfig; ``` 这里使用了@Autowired注解,表示需要自动装配MyConfig对象。 6. 如果使用了多个配置文件,需要确保它们没有冲突,例如: ``` @PropertySources({ @PropertySource("classpath:application.properties"), @PropertySource("classpath:other.properties") }) public class AppConfig { // ... } ``` 这里使用了@PropertySources注解,可以配置多个配置文件。需要确保它们没有相同的属性名,否则会出现冲突。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值