还不懂spring中的bean的话,你一定得好好看看这篇文章
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的话,你一定得好好看看这篇文章的更多相关文章
- spring扩展点之二:spring中关于bean初始化、销毁等使用汇总,ApplicationContextAware将ApplicationContext注入
<spring扩展点之二:spring中关于bean初始化.销毁等使用汇总,ApplicationContextAware将ApplicationContext注入> <spring ...
- 第2章 Spring中的Bean
2.1 Bean的配置 Bean本质是Java中的类.Spring可以被看做一个大型工厂,这个工厂的作用就是生产和管理Spring容器zho中的Bean.想在项目中使用这个工厂,就需要对Spring的 ...
- 【Spring】Spring中的Bean - 5、Bean的装配方式(XML、注解(Annotation)、自动装配)
Bean的装配方式 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 文章目录 Bean的装配方式 基于XML的装配 基于注解 ...
- 【Spring】Spring中的Bean - 3、Bean的作用域
Bean的作用域 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 通过Spring容器创建一个Bean的实例时,不仅可以完成 ...
- 【Spring】Spring中的Bean - 2、Baen的实例化 (构造器、静态工厂、实例工厂)
Bean的实例化 文章目录 Bean的实例化 构造器实例化 静态工厂方式实例化 实例工厂方式实例化 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-S ...
- spring中bean的五种作用域?Spring中的bean是线程安全的吗?
spring中bean的五种作用域 当通过spring容器创建一个Bean实例时,不仅可以完成Bean实例的实例化,还可以为Bean指定特定的作用域.Spring支持如下5种作用域: singleto ...
- JSP访问Spring中的bean
JSP访问Spring中的bean <%@page import="com.sai.comment.po.TSdComment"%> <%@page import ...
- 传统javabean与spring中的bean的区别
javabean已经没人用了 springbean可以说是javabean的发展, 但已经完全不是一回事儿了 用处不同:传统javabean更多地作为值传递参数,而spring中的bean用处几乎无处 ...
- 1.2(Spring学习笔记)Spring中的Bean
一.<Bean>的属性及子元素 在1.1中我们对<Bean>有了初步的认识,了解了一些基本用法. 现在我们进一步理解<Bean>属性及子元素. 我们先来看下< ...
随机推荐
- 51node1256 乘法匿元(扩展欧几里得)
#include<iostream> using namespace std; int gcd(int a,int b,int &x,int &y){ if (b==0){ ...
- VMware Workstatition启动虚拟机电脑蓝屏
电脑出了点问题,重装了系统,结果安装VMware之后,一启动虚拟机电脑就蓝屏重启. 系统是win10 19041 开始用的原来下载的vmware15.0,创建虚拟机蓝屏,重启之后可以创建.创建完以后启 ...
- linux中root目录下下指定磁盘空间扩容
1 查看当前磁盘情况 fdisk -l /dev/sda1 2048 6143 2048 83 Linux /dev/sda2 * 6144 1054719 524288 83 Linux /dev/ ...
- 微服务nacos服务注册与发现
一,以上一篇为基础 微服务从nacos配置中心获得配置信息 给service1, service2添加依赖 <dependency> <groupId>com.alibaba. ...
- openresty下安装luarocks
wget https://luarocks.org/releases/luarocks-2.4.1.tar.gz tar -xzvf luarocks-2.4.1.tar.gz cd luarocks ...
- Vue-cli3以上安装jquery
vue-cli3以上就没有webpack.config.js这个文件了,所以在安装jquery时 终端执行 npm install jquery --save 之后查看package.json 安装 ...
- unordered_set
用哈希表实现的 https://blog.csdn.net/dream_you_to_life/article/details/46785741
- Zotero使用教程
之前一直想有一个管理文献的好工具,但囿于麻烦都没有去做.最近需要阅读大量的文献,便重新拾起了这个念头,在几经搜索后,选定了Zotero作为文献管理工具. 至于为什么选择这个软件,我也许并说不清,网上有 ...
- Luogu P3602 Koishi Loves Segments
传送门 题解 既然是选取区间,没说顺序 肯定先排遍序 都是套路 那么按什么排序呢??? 为了方便处理 我们把区间按左端点从小到大排序 把关键点也按从小到大排序 假设当扫到 \(i\) 点时,i 点之前 ...
- STM32入门系列-STM32外设地址映射
片上外设区分为四条总线,根据外设速度的不同,不同总线挂载着不同的外设,APB1挂载低速外设,APB2和AHB挂载高速外设.相应总线的最低地址我们称为该总线的基地址,总线基地址也是挂载在该总线上的首个外 ...