Bean的装配方式
2022/3/8 23:15:41
本文主要是介绍Bean的装配方式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Bean的装配方式
1.基于XML的装配(设值注入和构造注入)
设值注入要求一个Bean满足以下两点要求:
-
Bean类必须提供一个默认的无参构造方法
-
Bean类必须为需要注入的属性提供对应的setter方法
public class User { private String name; private Integer password; private List<String> list; public User() { } public User(String name, Integer password, List<String> list) { this.name = name; this.password = password; this.list = list; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", password=" + password + ", list=" + list + '}'; } public void setName(String name) { this.name = name; } public void setPassword(Integer password) { this.password = password; } public void setList(List<String> list) { this.list = list; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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.xsd"> <!-- 1.使用构造注入方式装配User实例 --> <bean id="user1" class="com.itheima.assemble.User"> <constructor-arg index="0" value="tom"/> <constructor-arg index="1" value="123456"/> <constructor-arg index="2"> <list> <value>"constructorvalue1"</value> <value>"constructorvalue2"</value> </list> </constructor-arg> </bean> <!-- 2.使用设值注入方式装配User实例 --> <bean id="user2" class="com.itheima.assemble.User"> <property name="name" value="张三"></property> <property name="password" value="123456"></property> <property name="list"> <list> <value>"setlistvalue1"</value> <value>"setlistvalue2"</value> </list> </property> </bean> </beans>
public class XmlBeanAssembleTest { public static void main(String[] args) { String xmlPath = "com/itheima/assemble/beans5.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); System.out.println(applicationContext.getBean("user1")); System.out.println(applicationContext.getBean("user2")); } }
2.基于Annotation的装配(推荐)
一些常用的注解:
@Repository:用于将数据访问层(Dao层)的类标识为Spring中的Bean
@Service:用于将业务层(Service层)的类标识为Spring中的Bean
@Controller:用于将控制层(如Spring MVC中的Controller层)的类标识为Spring中的Bean
@Autowired:用于对Bean的属性变量、属性的setter方法及构造方法进行标注,配合对应的注解处理器完成Bean的自动装配工作,默认按照Bean的类型进行装配
@Resource:作用和@Autowired一样,但默认按照Bean实例名称装配
public interface UserDao { public void save(); }
@Repository("userDao") public class UserDaoImpl implements UserDao { @Override public void save() { System.out.println("userDao...save()...."); } }
public interface UserService { public void save(); }
@Service("userService") public class UserServiceImpl implements UserService{ //@Resource(name = "userDao") @Autowired private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void save() { this.userDao.save(); System.out.println("userService ... save()..."); } }
@Controller("userController") public class UserController { //@Resource(name = "userService") @Autowired private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } public void save(){ this.userService.save(); System.out.println("userController... save()..."); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 方式一 --> <!-- 使用context命名空间,在配置文件中开启相应的注解处理器--> <context:annotation-config/> <!-- 可以使用bean元素的autowire属性完成自动装配 --> <bean id="userDao" class="com.itheima.annotation.UserDaoImpl"></bean> <bean id="userService" class="com.itheima.annotation.UserServiceImpl" autowire="byName"></bean> <bean id="userController" class="com.itheima.annotation.UserController" autowire="byName"></bean> <!-- 方式二,推荐 --> <!-- 使用context命名空间,通知Spring扫描指定包下所有Bean类,进行注解解析 --> <!-- <context:component-scan base-package="com.itheima.annotation"/>--> </beans>
public class AnnotationAssembleTest { public static void main(String[] args) { String xmlPath = "com/itheima/annotation/beans6.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); UserController userController = (UserController) applicationContext.getBean("userController"); userController.save(); } }
3.自动装配
Spring的<bean>元素中包含一个autowire属性,可以通过该属性的属性值进行自动装配Bean,常用的属性值有byName、byType,使用时要提供类属性的setter方法。
这篇关于Bean的装配方式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-01后台管理开发学习:新手入门指南
- 2024-11-01后台管理系统开发学习:新手入门教程
- 2024-11-01后台开发学习:从入门到实践的简单教程
- 2024-11-01后台综合解决方案学习:从入门到初级实战教程
- 2024-11-01接口模块封装学习入门教程
- 2024-11-01请求动作封装学习:新手入门教程
- 2024-11-01登录鉴权入门:新手必读指南
- 2024-11-01动态面包屑入门:轻松掌握导航设计技巧
- 2024-11-01动态权限入门:新手必读指南
- 2024-11-01动态主题处理入门:新手必读指南