一,bean的装配

  bean是依赖注入的,通过spring容器取对象的。

  装配方法有:

  

前面两种没什么好讲的,就改改参数就好了。

这里重要讲注解。

注解的主要类型见图,其中component是bean,repository,service,controller都是spring中的DAO层,service层和controller层的bean,autowired和resourcey用来对bean的属性进行标注。

一个注解的例子:

package com.itheima.annotation;

public interface UserDao {
public void save();
}
package com.itheima.annotation; @Repository("userDao")
public class UserDaoImpl implements UserDao{
public void save(){
System.out.println("userdao--save()");
}
}
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;
public void save(){
System.out.println("userservice...save..");
}
}

  

package com.itheima.annotation;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

@Controller("userController")
public class UserController {
@Resource(name = "userService")
private UserService userService;
public void save(){
this.userService.save();
System.out.println("UserController...save...");
}
}

 

xml配置文件配置context的属性

<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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.1.xsd
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">
<!-- 使用context,开启注解处理器 -->
<context:annotation-config />
<bean id = "userDao" class = "com.itheima.annotation.UserDaoImpl"/>
<bean id = "userService" class = "com.itheima.annotation.UserServiceImpl"/>
<bean id = "userController" class = "com.itheima.annotation.UserController"/>
</beans>

  

 测试代码

public class UserControllerTest {
@Test
public void testController(){
ClassPathXmlApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
//不是通过new来获取Dao对象,而是通过Spring如弄个其来获取实现类的对象
UserController userController = (UserController) applicationContext.getBean("userController");
userController.save();
}
}

  

运行结果

userservice...save..
UserController...save...

  

这里的bean还是要配置,有个更简单的方法,修改xml中的配置,直接读取package里面的bean,不用单独配置

	<context:component-scan base-package="com.itheima.annotation" />

  

第三种,Autowired自动装配

这种和resource几乎没什么差别,差别就是resource是用name装配,autowired用得type装配。

代码甩一部分:

@Service("userService")
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
public void save(){
System.out.println("userservice...save..");
}
}

  

	<context:component-scan base-package="com.itheima.annotation" />

	<bean id = "userDao" class = "com.itheima.annotation.UserDaoImpl" autowire="byName"/>
<bean id = "userService" class = "com.itheima.annotation.UserServiceImpl" autowire="byName"/>
<bean id = "userController" class = "com.itheima.annotation.UserController" autowire="byName"/>

  

Spring -bean的装配和注解的使用的更多相关文章

  1. Spring Bean 的装配方式

    Spring Bean 的装配方式 装配 Bean 的三种方式 一个程序中,许多功能模块都是由多个为了实现相同业务而相互协作的组件构成的.而代码之间的相互联系又势必会带来耦合.耦合是个具有两面性的概念 ...

  2. spring 学习(二):spring bean 管理--配置文件和注解混合使用

    spring 学习(二)spring bean 管理--配置文件和注解混合使用 相似的,创建 maven 工程,配置pom.xml 文件,具体可以参考上一篇博文: sprint 学习(一) 然后我们在 ...

  3. [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  4. Spring的自动装配和注解

    Bean的自动装配 自动装配说明 自动装配是使用spring满足bean依赖的一种方法 spring会在应用上下文中为某个bean寻找其依赖的bean. Spring的自动装配需要从两个角度来实现,或 ...

  5. Spring Bean的装配

    Bean 的装配,即Bean对象的创建.容器根据代码要求创建Bean对象后再传递给代码的过程,称为Bean的装配. 一.默认分的装配方式 默认的装配的方式调用Bean类的构造方法 二.动态工厂Bean ...

  6. spring——bean自动装配

    注意:自动装配功能和手动装配要是同时使用,那么自动装配就不起作用. beans.xml <?xml version="1.0" encoding="UTF-8&qu ...

  7. day38 16-Spring的Bean的装配:注解的方式

    Struts 2和hibernate也使用注解,但是使用注解在以后的开发中应用不多.但是可以说在整合的时候如何进行注解开发.在Spring中,注解必须会玩. package cn.itcast.spr ...

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

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

  9. Spring Bean 有关的那些注解

    尊重原著直接贴链接 https://mp.weixin.qq.com/s/7lhpEo73KG3-xPgbFiaLHw

随机推荐

  1. python 面试题

    1.os.path与sys.path的区别是什么? os.path 主要用于系统文件路径的操作 sys.path 主要是python解释器的系统环境参数的操作 2.re模块中match和search方 ...

  2. python正常时间和unix时间戳时间的相互转换源码

    在学习过程,将内容过程比较常用的一些内容做个珍藏,下面的内容段是关于python正常时间和unix时间戳时间的相互转换的内容,应该是对各朋友有些帮助. import time def timestam ...

  3. (最完美)MIUI12系统的Usb调试模式在哪里开启的步骤

    当我们使用安卓手机通过数据线链接到Pc的时候,或者使用的有些app比如我们公司营销小组当使用的app引号精灵,之前的老版本就需要开启usb调试模式下使用,现当新版本不需要了,如果手机没有开启usb调试 ...

  4. asp.net core 集成 log4net 日志框架

    asp.net core 集成 log4net 日志框架 Intro 在 asp.net core 中有些日志我们可能想输出到数据库或文件或elasticsearch等,如果不自己去实现一个 Logg ...

  5. Python使用Plotly绘图工具,绘制饼图

    今天我们来学习一下如何使用Python的Plotly绘图工具,绘制饼图 使用Plotly绘制饼图的方法,我们需要使用graph_objs中的Pie函数 函数中最常用的两个属性values,用于赋值给需 ...

  6. selenium-webdriver的二次封装(十)

    接着上篇随笔 selenium-配置文件定位元素 ,进行了配置文件设置后,将配置文件运用到定位元素中 思路:拿到定位的 key 和 value 后,对 webdrvier 中定位进行封装,使可以直接运 ...

  7. jquery 同步加载

    jquery在前端展示时,如果需要从服务器获取信息然后在更新,需要设置同步加载. async属性设置为:false即可. $.ajax({ url : 'test.php', type : 'post ...

  8. nginx性能优化(针对于高并发量仅供参考,并不是方案)

    目录 关于nginx.conf中的优化 配置nginx客户端网页缓存本地时间 nginx日志切割 nginx连接超时优化 Nginx 实现网页压缩功能 Nginx 实现防盗链功能 为目录添加访问控制 ...

  9. 教你在浏览器里做出EXCEL的效果

    在浏览器里做出EXCEL的效果,复制.粘贴.设置公式.双击编辑等效果,如果自己开发的话,比较麻烦,建议使用成熟的插件.这里介绍使用智表ZCELL插件,实现用户快捷操作. 首先下载插件,引入到页面中,一 ...

  10. 基于令牌桶算法实现的SpringBoot分布式无锁限流插件

    本文档不会是最新的,最新的请看Github! 1.简介 基于令牌桶算法和漏桶算法实现的纳秒级分布式无锁限流插件,完美嵌入SpringBoot.SpringCloud应用,支持接口限流.方法限流.系统限 ...