java依赖注入 注解,依赖注入(注解形式) - chenruibing的个人空间 - OSCHINA - 中文开源技术交流社区...

3d9597773b461725ef0ed2ebcd6dbf78.png

步骤

1ac98505802a956387bc35612b0009fa.png

配置文件配置

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容器管理

54c10f746ecedc7b8381d6a240db7d52.png

3e7fa802744d50152354a20a725db16e.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值