天天看點

spring (使用@autowired注解)

cn.dao

[java]  view plain copy

  1. <pre name="code" class="java">package cn.dao;  
  2. public interface PersonDaoInterface {  
  3.     public abstract void add();  
  4. }  

cn.dao.imp

[java]  view plain copy

  1. package cn.dao.imp;  
  2. import cn.dao.PersonDaoInterface;  
  3. public class PersonDao implements PersonDaoInterface {  
  4.     public void add()  
  5.     {  
  6.         System.out.println("執行PersonDao.add()");  
  7.     }  
  8. }  

cn.service

[java]  view plain copy

  1. package cn.service;  
  2. public interface PersonService {  
  3.     public abstract void save();  
  4. }  

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 cn.dao.imp.PersonDao;
import cn.service.PersonService;

public class PersonServiceBean implements PersonService {
	
	private String name;
	@Autowired private PersonDao personDao;
	
	public PersonServiceBean(){}
	
	public PersonServiceBean(String name,PersonDao personDao) {
		this.name = name;
		this.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();
		//System.out.println(name);
		
	}
}
           

也可以寫成這樣

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 cn.dao.imp.PersonDao;
import cn.service.PersonService;

public class PersonServiceBean implements PersonService {
	
	private String name;
	@Autowired @Qualifier("personDao") private PersonDao personDao;
	
	public PersonServiceBean(){}
	
	public PersonServiceBean(String name,PersonDao personDao) {
		this.name = name;
		this.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();
		//System.out.println(name);
		
	}
}
           

juintest

[java]  view plain copy

  1. package junit.test;  
  2. import org.junit.Test;  
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.AbstractApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6. import cn.service.imp.PersonServiceBean;  
  7. public class SpringTest   
  8. {  
  9.     @Test   
  10.     public void instanceSpring()  
  11.     {  
  12.         AbstractApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"});  
  13.         PersonServiceBean personServiceBean=(PersonServiceBean) ctx.getBean("personService");  
  14.         personServiceBean.save();  
  15.         ctx.close();  
  16.     }  
  17. }  

bean.xml

[html]  view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.            http://www.springframework.org/schema/context  
  8.            http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  9.         <context:annotation-config></context:annotation-config>  
  10.         <bean id="personDao" class="cn.dao.imp.PersonDao"></bean>  
  11.         <bean id="personService" class="cn.service.imp.PersonServiceBean">  
  12.         </bean>  
  13. </beans>