Spring Bean装载方式代码实例解析

网友投稿 221 2022-12-15


Spring Bean装载方式代码实例解析

这篇文章主要介绍了Spring Bean装载方式代码实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

Bean的装配方式

Bean的装配可以理解为依赖关系注入

基于XML的装配

a) 设值注入

i.要求:

Bean 类必须提供一个默认的无参构造方法。

Bean 类必须为需要注入的属性提供对应的setter方法。

b) 构造注入

package com.itheima.assemble;

import java.util.List;

public class User {

private String username;

private Integer password;

private List List;

/*

* 1.使用构造注入

* 1.1提供所有带参数的有参构造方法

*/

public User(String username,Integer password,List List){

super();

this.username = username;

this.password = password;

this.List = List;

}

/*

* 2.使用设值注入

* 2.1提供默认空构造方法

* 2.2为所有属性提供setter方法

*/

public User(){

}

public void setUsername(String username) {

this.username = username;

}

public void setPassword(Integer password) {

this.password = password;

}

public void setList(List list) {

List = list;

}

@Override

/*

* (non-Javadoc)

* @see java.lang.Object#toString()

* 为了输出是看到结果,重写toString()方法

*/

public String toString() {

return "User [username=" + username + ", password=" + password + ", List=" + List + "]";

}

}

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

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/hpABcvDGsrschema/beans/spring-beans.xsd">

"constructorvalue1"

"constructorvalue2"

"setlistvalue1"

"setlistvalue2"

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

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/hpABcvDGsrschema/beans/spring-beans.xsd">

"constructorvalue1"

"constructorvalue2"

"setlistvalue1"

"setlistvalue2"

元素用于定义构造方法的参数,子元素来为Use r 类中对应的list集合属性注入值。

其中元素用于调用Bean实例中的setter方法完成属性赋值,从而完成依赖注入。

package com.itheima.assemble;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class XmlBeanAssembleTest {

public static void main(String[] args) {

//定义配置文件路径

String xmlPath = "com/itheima/assemble/beans5.xml";

//加载配置文件

ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);

//构造方式输出结果

System.out.println("构造方式:");

System.out.println(applicationContext.getBean("user1"));

//设值方式输出结果

System.out.println("设值方式:");

System.out.println(applicationContext.getBean("user2"));

}

}

2.基于Annotation的装配

package com.itheima.annotation;

public interface UserDao {

public voidhttp:// save();

}

package com.itheima.annotation;

import org.springframework.stereotype.Repository;

@Repository("userDao")

public class UserDaoImpl implements UserDao{

public void save(){

System.out.println("userdao...save...");

}

}

先使用@Repository 注解将UserDaolmpl 类标识为Spring 中的Bean,其写法相当于配置文件中

package com.itheima.annotation;

public interface UserService {

public void save();

}

package com.itheima.annotation;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

@Service("userService")

public class UserServiceImpl implements UserService{

@Resource(name="userDao")

private UserDao userDao;

@Override

public void save() {

// TODO Auto-generated method stub

//调用userDao中的save()方法

this.userDao.save();

System.out.println("userservice...save...");

}

public void setUserDao(UserDao userDao) {

this.userDao = userDao;

}

}

@Service 注解将UserServicelmpl 类标识为Spring中的Bean,这相当于配置文件中

public class UserController {

@Resource(name="userService")

private UserService userService;

public void save(){

this.userService.save();

System.out.println("userControlle...save...");

}

public void setUserService(UserService userService) {

this.userService = userService;

}

}

Controller 注解标注了UserController 类,这相当于在配置文件中编写; 然后使用了@Resource 注解标注在userService 属性上,这相当于在配置文件中编写

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.3.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.3.xsd">

package com.itheima.annotation;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AnnotationAssembleTest {

public static void main(String[] args) {

String xmlPath = "com/itheima/annotation/beans6.xml";

ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);

//获取UserController实例

UserController userController = (UserController)applicationContext.getBean("userController");

//调用UserController中的save()方法

userController.save();

}

}

3.自动装配

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.3.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.3.xsd">

增加了autowire 属性,并将其属性值设置为byName 。在默认情况下,配置文件中需要通过ref 来装配Bean ,但设置了autowire=" byName"后,Spring 会自动寻找userServiceBean 中的属性,并将其属性名称与配置文件中定义的Bean 做匹配。由于UserServicelmpl 中定义了userDao 属'性及其setter 方法,这与配置文件中id 为userDao 的Bean 相匹配,所以Spring会自动地将id 为userDao 的Bean 装配到id 为userService 的Bean 中。

public class UserController {

@Resource(name="userService")

private UserService userService;

public void save(){

this.userService.save();

System.out.println("userControlle...save...");

}

public void setUserService(UserService userService) {

this.userService = userService;

}

}

Controller 注解标注了UserController 类,这相当于在配置文件中编写; 然后使用了@Resource 注解标注在userService 属性上,这相当于在配置文件中编写

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.3.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.3.xsd">

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.3.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.3.xsd">

package com.itheima.annotation;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AnnotationAssembleTest {

public static void main(String[] args) {

String xmlPath = "com/itheima/annotation/beans6.xml";

ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);

//获取UserController实例

UserController userController = (UserController)applicationContext.getBean("userController");

//调用UserController中的save()方法

userController.save();

}

}

3.自动装配

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.3.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.3.xsd">

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.3.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.3.xsd">

增加了autowire 属性,并将其属性值设置为byName 。在默认情况下,配置文件中需要通过ref 来装配Bean ,但设置了autowire=" byName"后,Spring 会自动寻找userServiceBean 中的属性,并将其属性名称与配置文件中定义的Bean 做匹配。由于UserServicelmpl 中定义了userDao 属'性及其setter 方法,这与配置文件中id 为userDao 的Bean 相匹配,所以Spring会自动地将id 为userDao 的Bean 装配到id 为userService 的Bean 中。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:springboot乱码问题解决方案
下一篇:JAVA错误类结果类和分页结果类代码详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~