bean的装配方式有两种,构造注入和setter属性注入。

public class User {
private String username;
private String password;
private List<String> list;
//构造注入需要提供带所有参数的有参构造方法
public User(String username, String password, List<String> list) {
this.username = username;
this.password = password;
this.list = list;
}
//设置注入,需要提供无参构造方法和setter和getter方法
public User() {
} public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public List<String> getList() {
return list;
} public void setList(List<String> list) {
this.list = list;
} @Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", list=" + list +
'}';
}
} 在applicationContext.xml中的配置
<!--构造方法方式装配bean-->
<!--在这儿可以采用p名称空间的形式注入值-->
<bean id="user1" class="com.itheima.ioc.xmlzhuangpeiBean.User">
<constructor-arg index="0" value="chen"/>
<constructor-arg index="1" value="123456"/>
<constructor-arg index="2">
<list>
<value>"user1"</value>
<value>"user2"</value>
</list>
</constructor-arg>
</bean>
<!--使用设置注入方式装配bean--> <!--在这儿可以采用p名称空间的形式注入值-->
    <bean id="user2" class="com.itheima.ioc.xmlzhuangpeiBean.User">
<property name="username" value="陈红君"/>
<property name="password" value="145678"/>
<property name="list">
<list>
<value>"4679"</value>
<value>"213"</value>
</list>
</property>
</bean>
<!--例如这儿的bean可以写成这样
<bean id="user2" class="类的全限定名" p:username="小城" p:password="123"/>
--> </beans> 基于注解的装配
接口UserDao
public interface UserDao {
public void userDaoSay(); }
实现接口的USerDaoImp
@Repository("userDao")//用于数据访问层
public class UserDaoImp implements UserDao {
@Override
public void userDaoSay() {
System.out.println("这是实现userdao的方法");
}
}
接口UserService 
public interface UserService {
public void userServiceSay();
}
实现接口的UserServiceImp
@Service("userService")
public class UserServiceImp implements UserService {
@Resource(name = "userDao")//按照实例名称进行装配的
private UserDao userDao; public void userServiceSay() {
this.userDao.userDaoSay();
System.out.println("userservice to say hello world!"); }
}
控制器UserController
@Controller("userController")//控制器
public class UserController {
@Resource(name = "userService")
private UserService userService; public void userControllerSay() {
this.userService.userServiceSay();
System.out.println("userController to Say hello world!");
} /*实例化一个userController对象,调用userControllerSay()方法,userController 先调用userService里的方法
当执行到了userService,userService就去调用执行userDao中的userDaoSay()方法
* */
}
applicationContext.xml中的配置
<?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-3.0.xsd"> <context:component-scan base-package="com.itheima.ioc.zhujiefangshizhuangpeiBean"/> </beans>

测试
public class annotationTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("anntationContext.xml");
UserController userController = (UserController) applicationContext.getBean("userController");
userController.userControllerSay();
}
}
												

bean的装配方式(注入方式,构造注入,setter属性注入)的更多相关文章

  1. Spring Bean自动装配有哪些方式?

    Spring 容器能够自动装配 Bean .也就是说,可以通过检查 BeanFactory 的内容让 Spring 自动解析 Bean 的协作者. 自动装配的不同模式: no - 这是默认设置,表示没 ...

  2. Spring注入值得2种方式:属性注入和构造注入

    Spring是一个依赖注入(控制反转)的框架,那么依赖注入(标控制反转)表现在那些地方了? 即:一个类中的属性(其他对象)不再需要手动new或者通过工厂方法进行创建,而是Spring容器在属性被使用的 ...

  3. Spring学习(十八)Bean 的三种依赖注入方式介绍

    依赖注入:让调用类对某一接口实现类的依赖关系由第三方注入,以移除调用类对某一接口实现类的依赖.接下来将详细的向大家介绍Spring容器支持的三种依赖注入的方式以及具体配置方法:•    属性注入方法• ...

  4. 【SSH系列】深入浅出spring IOC中三种依赖注入方式

    spring的核心思想是IOC和AOP,IOC-控制反转,是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,控制反转一般分为两种类型,依赖注入和依赖查找,依赖什么?为什么需要依赖?注入什么?控 ...

  5. spring IOC中三种依赖注入方式

    Spring的核心思想是IOC和AOP,IOC-控制反转,是一个重要的面向对象编程的法则,用来消减计算机程序之间的耦合问题,控制反转一般分为两种类型,依赖注入和依赖查找,依赖什么?为什么需要依赖?注入 ...

  6. spring IOC中四种依赖注入方式

    在spring ioc中有三种依赖注入,分别是:https://blog.csdn.net/u010800201/article/details/72674420 a.接口注入:b.setter方法注 ...

  7. Spring 依赖注入(一、注入方式)

    Spring是一个依赖注入(控制反转)的框架,那么依赖注入(标控制反转)表现在那些地方了? 即:一个类中的属性(其他对象)不再需要手动new或者通过工厂方法进行创建,而是Spring容器在属性被使用的 ...

  8. 转:深入浅出spring IOC中四种依赖注入方式

    转:https://blog.csdn.net/u010800201/article/details/72674420 深入浅出spring IOC中四种依赖注入方式 PS:前三种是我转载的,第四种是 ...

  9. SpringBoot入门一:基础知识(环境搭建、注解说明、创建对象方法、注入方式、集成jsp/Thymeleaf、logback日志、全局热部署、文件上传/下载、拦截器、自动配置原理等)

    SpringBoot设计目的是用来简化Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,SpringBoot致力于在蓬勃发 ...

随机推荐

  1. JSF action actionListner 详解

    https://stackoverflow.com/questions/3909267/differences-between-action-and-actionlistener   actionLi ...

  2. Vue中data返回对象和返回值的区别

    速记:粗浅的理解是,事件的结果是影响单个组件还是多个组件.因为大部分组件是要共享的,但他们的data是私有的,所以每个组件都要return一个新的data对象 返回对象的时候 <!DOCTYPE ...

  3. 第四篇 - 爬取前程无忧python相关工作

    环境:python3    pycharm 模块:requests,xlwt,urllib.request,re 正常三步走: 1.获取源代码 2.匹配源代码,获得目标数据 3.存储到文件中 直接上代 ...

  4. TreeView CheckBox勾选联动

    http://www.cnblogs.com/excellently/p/TreeViewCheckBox.html 在C# Winform项目中用到了TreeView控件,并且需要勾选的功能.父子节 ...

  5. Java实现二叉树的前序、中序、后序、层序遍历(递归方法)

      在数据结构中,二叉树是树中我们见得最多的,二叉查找树可以加速我们查找的效率,那么输出一个二叉树也变得尤为重要了.   二叉树的遍历方法分为四种,分别为前序遍历.中序遍历.后序.层序遍历.下图即为一 ...

  6. 【译】3. Java反射——构造函数

    原文地址:http://tutorials.jenkov.com/java-reflection/constructors.html ================================= ...

  7. 交叉编译jpeglib遇到的问题

    由于要在开发板中加载libjpeg,不能使用gcc编译的库文件给以使用,需要自己配置使用另外的编译器编译该库文件. /usr/bin/ld: .libs/jaricom.o: Relocations  ...

  8. day15-ajax和jquery

    回顾: 分页: 将数据按照页码划分,提高用户的体验度. 分类: 逻辑分页:一次性将内容加载到内存(list),获取自己想要的数据 sublist截取.缺点:维护起来麻烦 物理分页:(经常使用) 每次只 ...

  9. Redash 安装部署

    介绍 是一款开源的BI工具,提供了基于web的数据库查询和数据可视化功能. 官网:https://redash.io/ GitHub:https://github.com/getredash/reda ...

  10. 剑指Offer_编程题_8

    题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. class Solution { public: int jumpFloor(int number ...