package com.free.dao;public interface PersonDao { public abstract void add();}package com.free.dao.impl;import com.free.dao.PersonDao;public class PersonDaoBean implements PersonDao {
public void add(){
System.out.println("执行PersonDaoBean中的add()方法");
}
}
<?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:annotation-config/>
<bean id="personDaos" class="com.free.dao.impl.PersonDaoBean" ></bean>
<bean id="personService" class="com.free.service.impl.PersonServiceBean" >
</bean><!-- 交给spring管理的bean 默认是单实例的 直接加载的 -->
</beans>
package junit.test;import junit.framework.TestCase;import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.free.service.PersonService;
public class SpringTest extends TestCase {
public void testInsertSpring(){
AbstractApplicationContext ac =new ClassPathXmlApplicationContext("beans.xml");
PersonService ps=(PersonService)ac.getBean("personService");
ps.save();
}
}package com.free.service.impl;
import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import com.free.dao.PersonDao;
import com.free.dao.impl.PersonDaoBean;
import com.free.service.PersonService;public class PersonServiceBean implements PersonService {
@Resource(type=PersonDaoBean.class) private PersonDao personDao;
private String name;
public void init(){
System.out.println("init.......");
}
public PersonServiceBean() {
}
public void save(){
System.out.println(name);
personDao.add();
System.out.println("我是save方法");
}
public void destory(){
System.out.println("关闭打开的资源");
}
}小弟不才,刚学spring求教大大们。小弟特地把bean的name改成不一样,给@Resource按类名注射怎么报错了求999999999999
ERROR如下:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personService': Injection of resources failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'personDao' is defined
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'personDao' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:353)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:916)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:243)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:197)
at org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.injectResource(CommonAnnotationBeanPostProcessor.java:278)
at org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor$ResourceMetadata.injectResources(CommonAnnotationBeanPostProcessor.java:215)
at org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor.postProcessAfterInstantiation(CommonAnnotationBeanPostProcessor.java:139)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:412)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:287)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:91)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:75)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:65)
at junit.test.SpringTest.testInsertSpring(SpringTest.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
@Service(value = "personDao")
public class PersonDaoBean implements PersonDao {
public void add(){
System.out.println("执行PersonDaoBean中的add()方法");
}
}public class PersonServiceBean implements PersonService {
@Resource
private PersonDao personDao;
private String name;
public void init(){
System.out.println("init.......");
}
public PersonServiceBean() {
}
public void save(){
System.out.println(name);
personDao.add();
System.out.println("我是save方法");
}
public void destory(){
System.out.println("关闭打开的资源");
}
}
<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:annotation-config/>
<context:component-scan base-package="com.free" />
</beans>
@Resource(type=PersonDaoBean.class)
private PersonDao personDao
该为:
@Resource("personDaos") --->就是在xml文件中的实现类的Id或是name 的值
private PersonDao personDao
想用annotion的话,就全部使用吧
AbstractApplicationContext ac =new ClassPathXmlApplicationContext("beans.xml");
PersonService ps=(PersonService)ac.getBean("personService");
ps.save();
}运行的话写个main方法,测试一下。
这个是junit的方法 可以云新的