bean的作用域

bean的生命周期

bean的装配

代码

实体类

package com;

import java.util.List;

public class User {

    private String username;
private String password;
private List<String> list;
/**
* 设值注入要求bean类:
* 1、必须提供默认无参构造方法
* 2、为属性提供setter方法
*/
public User() {
} public User(String username, String password, List<String> list) {
this.username = username;
this.password = password;
this.list = list;
}
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 +
'}';
} }

dao层

package com;

import org.springframework.stereotype.Repository;

@Repository("userDao1")
public class UserDaoImpl implements UserDao{
public void say() {
System.out.println("dao");
}
}

service层

package com;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("userService")
public class UserServiceImpl implements UserService{ @Resource(name="userDao1")
// @Autowired
private UserDao userDao; public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} @Override
public void save() {
this.userDao.say();
System.out.println("service");
}
}

controller层

package com;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

@Controller("userController")
public class MyController { @Resource(name="userService")
// @Autowired
UserService userService; public void setUserService(UserService userService) {
this.userService = userService;
} public void say(){
this.userService.save();
System.out.println("controller");
}
}

基于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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <bean id="userDao" class="com.UserDaoImpl"/> <!-- 使用构造注入方式装配bean -->
<bean id="user1" class="com.User">
<constructor-arg index="0" value="zhangsan" />
<constructor-arg index="1" value="123" />
<constructor-arg index="2">
<list>
<value>construct1</value>
<value>construct2</value>
</list>
</constructor-arg>
</bean>
<!-- 使用设值注入方式装配bean -->
<bean id="user2" class="com.User">
<property name="username" value="lisi" />
<property name="password" value="123" />
<property name="list">
<list>
<value>set1</value>
<value>set2</value>
</list>
</property> </bean>
</beans>

基于Annotation装配

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <bean id="userDao1" class="com.UserDaoImpl" />
<bean id="userService" class="com.UserServiceImpl" />
<bean id="userController" class="com.MyController" />
<!-- 开启注解 -->
<context:annotation-config />
</beans>

自动装配

<!--       扫描包-->
<context:component-scan base-package="com"/>

小结

然后用@Autowired注解标注在属性上就可以实现自动装配了

还不懂spring中的bean的话,你一定得好好看看这篇文章的更多相关文章

  1. spring扩展点之二:spring中关于bean初始化、销毁等使用汇总,ApplicationContextAware将ApplicationContext注入

    <spring扩展点之二:spring中关于bean初始化.销毁等使用汇总,ApplicationContextAware将ApplicationContext注入> <spring ...

  2. 第2章 Spring中的Bean

    2.1 Bean的配置 Bean本质是Java中的类.Spring可以被看做一个大型工厂,这个工厂的作用就是生产和管理Spring容器zho中的Bean.想在项目中使用这个工厂,就需要对Spring的 ...

  3. 【Spring】Spring中的Bean - 5、Bean的装配方式(XML、注解(Annotation)、自动装配)

    Bean的装配方式 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 文章目录 Bean的装配方式 基于XML的装配 基于注解 ...

  4. 【Spring】Spring中的Bean - 3、Bean的作用域

    Bean的作用域 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 通过Spring容器创建一个Bean的实例时,不仅可以完成 ...

  5. 【Spring】Spring中的Bean - 2、Baen的实例化 (构造器、静态工厂、实例工厂)

    Bean的实例化 文章目录 Bean的实例化 构造器实例化 静态工厂方式实例化 实例工厂方式实例化 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-S ...

  6. spring中bean的五种作用域?Spring中的bean是线程安全的吗?

    spring中bean的五种作用域 当通过spring容器创建一个Bean实例时,不仅可以完成Bean实例的实例化,还可以为Bean指定特定的作用域.Spring支持如下5种作用域: singleto ...

  7. JSP访问Spring中的bean

    JSP访问Spring中的bean <%@page import="com.sai.comment.po.TSdComment"%> <%@page import ...

  8. 传统javabean与spring中的bean的区别

    javabean已经没人用了 springbean可以说是javabean的发展, 但已经完全不是一回事儿了 用处不同:传统javabean更多地作为值传递参数,而spring中的bean用处几乎无处 ...

  9. 1.2(Spring学习笔记)Spring中的Bean

    一.<Bean>的属性及子元素 在1.1中我们对<Bean>有了初步的认识,了解了一些基本用法. 现在我们进一步理解<Bean>属性及子元素. 我们先来看下< ...

随机推荐

  1. Html+Ajax+Webservice 实现文件跨域上传

    1. 界面HTML <p >上传文件: <input id="zfiles" type="file" name="file" ...

  2. centos搭建离线epel源

    准备 有一个联网的机器,并已经配置好epel源 安装reposync和createrepo命令 yum install yum-utils #reposync在这里 yum install creat ...

  3. 预编译 - Js三部曲

    预编译前奏 imply global - 暗示全局变量, window - 全局域 任何变量未经声明即赋值,该变量为全局所有 window 一切声明的 "全局" 变量,归windo ...

  4. Angular双向绑定简单理解

    在使用Antd的时候,一直很好奇里面的双向绑定的自定义组件是怎么做的. 因为之前一直用,没有去细看文档. 今天抽空来简单的撸一下. 在ng中,()是单向数据流,从视图目标到数据源,[()]这样就是双向 ...

  5. 使用WxPusher给自己的个人微信发送提醒消息(WxPusher微信推送服务)

    1.背景 我们很多时候,我们在服务器上运行软件,发生一些业务异常,需要给我们发送一个及时的提醒,或者是使用一些耗时软件,比如抢车票,抢课,刷优惠券当任务运行成功以后,也需要及时的发送消息给自己 ,告诉 ...

  6. 给定 n 个字符串,求有多少字符串是其他字符串的前缀。

    1 #include <cstdio> 2 #include <set> 3 #include <iostream> 4 #include <cstring& ...

  7. BadBoy+JMeter应用过程中遇到的问题汇总

    前几天对HTTPS协议的系统做压力测试,用LoadRunner录制脚本过程中,系统登录后跳转的页面一直加载不出来,试了很多办法没解决这个问题.转而利用BadBoy+JMeter进行压测.以下为在运用B ...

  8. puk1251 最小生成树

    Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid ...

  9. STM32入门系列-启动文件介绍

    在启动文件内部使用的都是汇编语言,这个文件的作用是负责执行微控制器从"复位"到"开始执行 main 函数"中间这段启动时间所必须进行的工作.它完成的具体工作有: ...

  10. django支持多语言

    Django支持多语言切换 下面介绍下如何使网站或APP国际化,让其支持多种语言 . 官网 效果 1.创建locale文件夹 先在项目根目录下创建一个名为locale的文件夹,这个文件夹是用来存放dj ...