java 注解传参数 java注解注入_java依赖注入 注解

步骤

java 注解传参数 java注解注入_xml_02

配置文件配置

xmlns:context="http://www.springframework.org/schema/context"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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

student类,在配置文件配置bean了


package _4annotation.di;
public class Student {
public void show(){
System.out.println("i am a student");
}
} person类,使用resource注解说明student变量的实例化
package _4annotation.di;
import javax.annotation.Resource;
public class Person {
//不再使用set注入,构造器注入
//使用注解注入
@Resource(name="student")
private Student student;
public void show(){
this.student.show();
}
} 测试类
package _4annotation.di;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) context.getBean("person");
person.show();
}
}

扫描形式,自动扫描注解类纳入spring容器管理

java 注解传参数 java注解注入_mvc_03

java 注解传参数 java注解注入_xml_04

DAO接口

package _4annotation.mvc;
public interface PersonDao {
public void savePerson();
} DAO实现类,repository注解为bean
package _4annotation.mvc;
import org.springframework.stereotype.Repository;
@Repository("personDao")
public class PersonDaoImpl implements PersonDao{
@Override
public void savePerson() {
// TODO Auto-generated method stub
System.out.println("save person dao");
}
} service接口
package _4annotation.mvc;
public interface PersonService {
public void savePerson();
} service实现,service注解为bean,resource注解变量实例化name为指定值的bean
package _4annotation.mvc;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
@Service("personService")
public class PersonServiceImpl implements PersonService{
@Resource(name="personDao")
private PersonDao personDao;
@Override
public void savePerson() {
// TODO Auto-generated method stub
this.personDao.savePerson();
}
} Action,controller注解
package _4annotation.mvc;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
@Controller("personAction")
public class PersonAction {
@Resource(name="personService")
private PersonService personService;
public void savePerson(){
this.personService.savePerson();
}
} 测试类
package _4annotation.mvc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MVCTest{
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonAction personAction = (PersonAction)context.getBean("personAction");
personAction.savePerson();
}
}

配置文件

xmlns:context="http://www.springframework.org/schema/context"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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