一,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. Git日常须知

    基本操作: git init 初始化环境 git add . 管理文件 git status 查看状态 git diff 文件名 查看修改内容 git commit -m '' 提交文件 git lo ...

  2. rabbitmq之基本原理及搭建单机环境

    1.RabbitMQ基本原理 1.MQ全称Message Queue,是一种分布式应用程序的通信方法,是消费-生产者模型的典型代表,producer向消息队列中不断写入消息,而另一端consumer则 ...

  3. selenium-自动化用例(十一)

    思路 将页面操作与用例case分别封装,编写case时就可以用同一个操作方法对应多个case 如下图: PageGUI:存放页面操作方法,每个页面写一个文件,每个文件中写同一个页面不同的操作,例如检索 ...

  4. SQLServer之创建事务序列化

    创建事务序列化注意事项 语法:set transaction isolation level serialize; 序列化会指定下列内容: 语句不能读取已由其他事务修改但尚未提交的数据. 任何其他事务 ...

  5. SQL Server创建Job, 实现执行相同脚本而产生不同作业计划的探究

    1 . 背景描述 本公司的SQL Server 服务器近百台,为了收集服务器运行的状态,需要在各个实例上部署监控Job,将收集到的信息推送到中央管理服务器. 收集的信息主要包括:慢查询.阻塞.资源等待 ...

  6. SqlServer 操作 JSON

    SqlServer 操作 JSON Intro Sql Server 从 2016 开始支持了一些 json 操作,最近的项目里也是好多地方直接用字段直接存成了 json ,需要了解一下怎么在 Sql ...

  7. Python基础之注释,算数运算符,变量,输入和格式化输出

    Python的注释 注释的作用:用自己熟悉的语言,对某些代码进行标注说明,增强程序的可读性: 在python解释器解释代码的过程中,凡是#右边的,解释器都直接跳过这一行: 注释的分类 单行注释 # 这 ...

  8. Jquery消息提示插件toastr使用详解

    toastr是一个基于jQuery简单.漂亮的消息提示插件,使用简单.方便,可以根据设置的超时时间自动消失. 1.使用很简单,首选引入toastr的js.css文件 html <script s ...

  9. 弱网测试-Network Emulator 网络模拟工具使用

    参考链接 https://www.jianshu.com/p/6a3d38aafac1

  10. c++ 指针做为参数和返回值

    指针参数 返回值是指针 一.指针作参数形式的函数 //计算x的平方 x*x void square(int *x) { int a=*x; *x=a*a; } 二.指针作返回值的函数 int *squ ...