Spring (依赖注入(DI)的两种方式)

Spring入门:https://blog.csdn.net/young_1004/article/details/81827665

Spring 依赖注入的两种方式

1、使用XML注入

 

1.1使用setter()方法

在注入属性的时候框架会根据方法名字找到你要注入的属性名从而完成注入。在配置文件中如果按setter()方法注入属性的话,属性名要和应用类中的名称保持一致,不然Spring容器会找不见你当前所要注入的属性完成属性值的注入。

 

1、在 applicationContext.xml 中引入约束

2、创建应用类

注意:在创建应用类的时候如果使用setter(0方法注入要提供一个无参构造器,如果不写构造器,java会默认的提供一个无参构造器,如果你在类中写了构造器,java将不再为你创建无参构造器。

package com.ma.spring.demo01;



public class User {

private Integer age;

private String name;



public Integer getAge() {

return age;

}



public void setAge(Integer age) {

this.age = age;

}



public String getName() {

return name;

}



public void setName(String name) {

this.name = name;

}



@Override

public String toString() {

return "User{" +

"age=" + age +

", name='" + name + '\'' +

'}';

}

}

 

 

3、在配置文件中注入属性

<bean id = "user" class = "com.ma.spring.demo01.User" scope="singleton" >

<property name="age" value="20"></property>

<property name="name" value="Young"></property>

</bean>

 

 

4、测试类

package com.ma.spring.demo01;



import org.junit.Test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.beans.IntrospectionException;

import java.lang.reflect.Method;



public class UserTest {

@Test

public void test01() {

//1、创建Spring的工厂对象(BeanFactory ApplicationContext)

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

//2、使用applicationContext.xml内部配置的bean来创建对象

User user= (User) applicationContext.getBean("user");

System.out.println(user);

//applicationContext.close();

}

}

 

5、效果

 

 

1.2使用构造器注入属性

1、在 applicationContext.xml 中引入约束

 

2、创建应用类(在类中要创建有参构造方法同时要有无参构造(如果按无参构造的方式创建Bean)

package com.ma.spring.demo01;



public class User {

private Integer age;

private String name;

public User() {

}

public User(Integer age, String name) {

this.age = age;

this.name = name;

}



public Integer getAge() {

return age;

}



public void setAge(Integer age) {

this.age = age;

}



public String getName() {

return name;

}



public void setName(String name) {

this.name = name;

}



@Override

public String toString() {

return "User{" +

"age=" + age +

", name='" + name + '\'' +

'}';

}

}



3、在配置文件中注入属性

<bean id = "user" class = "com.ma.spring.demo01.User" scope="singleton" >

<!--使用构造器注入属性-->

<constructor-arg name="age" value="21"></constructor-arg>

<constructor-arg name="name" value="小王"></constructor-arg>

</bean>

 

 

4、测试类

package com.ma.spring.demo01;



import org.junit.Test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.beans.IntrospectionException;

import java.lang.reflect.Method;



public class UserTest {

@Test

public void test01() {

//1、创建Spring的工厂对象(BeanFactory ApplicationContext)

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

//2、使用applicationContext.xml内部配置的bean来创建对象

User user= (User) applicationContext.getBean("user");

System.out.println(user);

//applicationContext.close();

}

}

 

5、效果

 

 

2、使用注解注入

 

2.1按类型注入

@Autowired

 

UserService.class

package com.ma.spring.demo02;



import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;



@Service

public class UserService {

@Autowired

private UserDao userDao;

public void addUser(){

System.out.println("UserService......addUser()...");

userDao.insert();

}

public UserDao getUserDao() {

return userDao;

}



public void setUserDao(UserDao userDao) {

this.userDao = userDao;

}

}



UserDao.class

package com.ma.spring.demo02;



import org.springframework.stereotype.Repository;



@Repository

public class UserDao {

public void insert(){

System.out.println("UserDao.....insert()...");

}

}

测试类

package com.ma.spring.demo02;



import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;



public class UserServiceTest {

@Test

public void testAddUser(){

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

UserService userService = (UserService) applicationContext.getBean("userService");

userService.addUser();

}

}



结果





2.2按名称注入

UserService.class

package com.ma.spring.demo02;





import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.stereotype.Service;



@Service("userService")

public class UserService {

@Autowired

@Qualifier("userDao")

private UserDao userDao;

public void addUser(){

System.out.println("UserService......addUser()...");

userDao.insert();

}

public UserDao getUserDao() {

return userDao;

}



public void setUserDao(UserDao userDao) {

this.userDao = userDao;

}

}

UserDao.class

package com.ma.spring.demo02;



import org.springframework.stereotype.Repository;



@Repository("userDao")

public class UserDao {

public void insert(){

System.out.println("UserDao.....insert()...");

}

}

 

测试类

package com.ma.spring.demo02;



import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;



public class UserServiceTest {

@Test

public void testAddUser(){

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

UserService userService = (UserService) applicationContext.getBean("userService");

userService.addUser();

}

}

结果

 

3.XML与注解的选择

在注解还没有出现的时候,只能使用XML的方式

现在主流开发使用XML+注解混合使用

    XML:用来管理 Bean

    注解用来依赖注入

未来的趋势是纯注解,简化开发,但有一定的耦合。

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值