Spring学习(二)Spring的bean管理(XML)
Bean的实例化方式
1、在Spring里面通过配置文件创建对象
2、bean实例化的三种方式
第一种:使用类的无参数构造函数创建(最常用的方式,第2种和第3种方法一般不用)
如果类里面没有无参的构造函数,将会出现异常
第二种:使用静态工厂创建
(1)创建类的静态方法,返回类对象
第三种:使用实例工厂创建
(2)创建不是静态的方法,返回类的对象
3、bean标签常用属性
(1)id
id表示为该类起一个名字,id属性值一般为小写的类名id=user
id属性值,不能有特殊符号,为一个单独的单词(user_1错误)
根据id值得到配置对象
(2)class:创建对象所在类的全路径 (3)name:功能和id属性是一样的(现在name属性已经不用了,为了针对整合struts1)
但是id睡醒不可以有特殊符号,name可以有特殊符号
(4)scope:
默认是单实例的,可以不写,也可以写:
多实例必须要写scope="prototype",多实例的应用场景:配置action
4、属性注入方法:
4.1 JAVA的属性注入方法:
(1)创建对象的时候,向类里面的属性设置值
(2)属性注入的介绍(3种方式)
方法一:set方式(用的最多)
public class User{
private String name;
public void setName(String name){
this.name=name;
}
} 实现:User user=new User();
user,setName("Zhangsan"); 方法二:有参构造
public class user{
private String name;
public User(String name){
this.name=name;
}
} 实现:
User user=new User("ZhangSan");
第三种:使用接口注入
public interface Dao{
public void delete(String name);
}
public class DaoImpl implements Dao{
private String name;
public void delete(String name){
this.name=name
}
}
4.2、在Spring框架中只支持(1)set方法注入和(2)有参的构造注入
(1)set
Book类:
package cn.itcast.property;
public class Book {
private String bookname;
public void setBookname(String bookname) {
this.bookname = bookname;
}
public void demobook(){
System.out.println("booknmae="+bookname);
}
}
配置文件:
<!-- 使用set方法注入属性 -->
<bean id="book" class="cn.itcast.property.Book">
<!-- 注入属性值 name:类里面定义的属性名称 value:设置具体的值
-->
<property name="bookname" value="JAVA"></property>
</bean>
(2)有参构造
类:
package cn.itcast.property;
public class PropertyDemo1 {
private String username;
public PropertyDemo1(String username) {
this.username = username;
}
public void test1() {
System.out.println("username....."+username);
}
}
配置文件:
<bean id="demo" class="cn.itcast.property.PropertyDemo1">
<constructor-arg name="username" value="小王"></constructor-arg>
</bean>
4.3 注入对象类型的属性********重点掌握
1、创建service类和dao类
在service中得到dao的对象
2、具体实现过程
在service里面把dao作为类属性
生成dao类型属性的set方法
配置文件中注入关系
UserDao类:
package cn.itcast.service;
public class UserDao {
public void add(){
System.out.println("dao.....");
}
}
UserService类:
package cn.itcast.service;
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(){
System.out.println("service.....");
//在service中得到到对象,才能调用dao里面的方法
// UserDao dao=new UserDao();
// dao.add();
userDao.add();
}
}
配置文件:
P名称空间注入:(很少用)
注入复杂类型的属性:
1、数组
2、list集合
3、Map集合
4、Properties类型
<bean id="user" class="cn.itcast.property.Person">
<!--数组-->
<property name="arrs">
<list>
<value>张三</value>
<value>李四</value>
<value>王五</value>
</list>
</property>
<!--list-->
<property name="list">
<list>
<value>张三list</value>
<value>李四list</value>
<value>王五list</value>
</list>
</property>
<!--map-->
<property name="map">
<map>
<entry key="a" value="lucy"></entry>
<entry key="b" value="tom"></entry>
<entry key="c" value="kate"></entry>
</map>
</property>
<!--properties-->
<property name="properties">
<props>
<prop key="driverclass">com.mysql.jdbc.Driver</prop>
<prop key="username">root</prop>
<prop key="pwd">123</prop>
</list>
</property> </bean>
Ioc和DI区别:
Ioc:控制反转,把对象的创建交给spring进行配置
DI:依赖注入,向类里面的属性中,设置值
Ioc与DI的关系,依赖注入不可以单独存在,需要在Ioc的基础之上来完成依赖注入的操作
Spring的bean管理(注解)
注解介绍:
- 代码里面特殊的标记,使用注解可以完成功能
- 注解写法 @注解名称(属性名称=属性值),例如单元测试@Test
- 注解用在什么地方:类、方法、属性都可以使用注解
spring注解开发准备工作
- 1、导入jar包
(1)导入基本jar包
(2)导入aop的jap包
- 2、创建类,创建方法
- 3、创建Spring配置文件
(1)不用注解方式,引入约束 beans,即
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
(2)引入新的约束
- 4、开启注解扫描
<!-- 开启注解扫描
(1)到包里面扫描类、方法、睡醒上面是否有注解
-->
<context:component-scan base-package="cn.itcast"></context:component-scan>
- 使用注解创建对象(可以替换配置文件,但是不可以完全脱离配置文件)
1、在创建对象的类上面使用注解实现
@Component(value="user")
public class User {
}
测试代码:
ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
User user=(User) context.getBean("user");
System.out.println("user==="+user);
user.add();
2、spring中提供的注解方式除了
(1)@Component 还有三个衍注解,是为了让标注类本身的用途更加的清晰
(2)@Controller WEB 层
(3)@Service 业务层
(4)@Repository 持久层
这四个注解的功能是一样的
3、创建对象的单实例还是多实例
@Component(value="user")
@Scope(value="prototype")//singleton
public class User {
}
- 注解方式来注入属性
(配置文件只有一行:<context:component-scan base-package="cn.itcast"></context:component-scan>)
1、创建service类,创建dao类,在service中得到dao的类
注入属性第一个注解@Autowired(用的不多):不需要指定类
注入属性第二个注解@Resource(name="userDao"):要指定用的name
@Component(value="userDao")
public class UserDao {
public void add(){
System.out.println("dao ....");
}
}
UserService类
@Service(value="userService")
public class UserService {
//得到dao 对象
//1、在service中定义dao类型属性
// 在dao属性上面使用注解来完成对象注入
@Autowired
private UserDao userDao;
//2、使用注解,不用set方法
public void add(){
System.out.println("service......");
userDao.add();
}
}
第二种属性注入方式
@Service(value="userService")
public class UserService {
//得到dao 对象
//1、在service中定义dao类型属性
// 在dao属性上面使用注解来完成对象注入
@Resource(name="userDao")
private UserDao userDao;
//2、使用注解,不用set方法
public void add(){
System.out.println("service......");
userDao.add();
}
}
测试代码:
public void testDemo(){
ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
UserService user=(UserService) context.getBean("userService");
System.out.println("user==="+user);
user.add();
}
配置文件和注解混合使用
1、创建对象的操作使用配置文件方式实现
2、注入属性操作注解方式实现
3、例子在BookService中使用BookDao类和OrdersDao类
BookDao类
package cn.itcast.xmlann0;
public class BookDao {
public void book(){
System.out.println("bookDao....");
}
}
OrdersDao类
package cn.itcast.xmlann0;
public class OrdersDao {
public void buy(){
System.out.println("ordersbuy....");
}
}
BookService类
package cn.itcast.xmlann0; import javax.annotation.Resource; public class BookService {
@Resource(name="bookDao")
private BookDao bookDao;
@Resource(name="ordersDao")
private OrdersDao ordersDao;
public void add(){
System.out.println("service....");
bookDao.book();
ordersDao.buy();
}
}
bean2.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.xsd"> <!-- bean definitions here -->
<!-- 开启注解扫描
(1)到包里面扫描类、方法、睡醒上面是否有注解
-->
<context:component-scan base-package="cn.itcast"></context:component-scan>
<!-- 配置对象 -->
<bean id="bookDao" class="cn.itcast.xmlann0.BookDao"></bean>
<bean id="ordersDao" class="cn.itcast.xmlann0.OrdersDao"></bean>
<bean id="bookService" class="cn.itcast.xmlann0.BookService"></bean>
</beans>
测试代码:
@Test
public void testxmlanno(){
ApplicationContext context =new ClassPathXmlApplicationContext("bean2.xml");
BookService bookService=(BookService) context.getBean("bookService");
bookService.add();
}
结果:
service....
bookDao....
ordersbuy....
Spring学习(二)Spring的bean管理(XML)的更多相关文章
- Spring学习二----------IOC及Bean容器
© 版权声明:本文为博主原创文章,转载请注明出处 接口 用于沟通的中介物的抽象化 实体把自己提供给外界的一种抽象化说明,用以由内部操作分离出外部沟通方法,使其能被修改内部而不影响外界其他实体与其交互的 ...
- Spring学习笔记三:Bean管理
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6775827.html 一:如何使用Spring获取对象 1:定义bean类:要按照注入方式来定义对应的bea ...
- Spring学习(十一)-----Spring使用@Required注解依赖检查
Spring学习(九)-----Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种 ...
- Spring学习(六)-----Spring使用@Autowired注解自动装配
Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...
- IoC容器-Bean管理XML方式(创建对象和set注入属性,有参构造注入属性)
Ioc操作Bean管理 1,什么是Bean管理 (0)Bean管理指的是两个操作 (1)Spring创建对象 (2)Spring注入属性 2,Bean管理操作有两种方式 (1)基于xml配置文件方式实 ...
- Spring基础学习(二)—详解Bean(上)
在Spring配置文件中,用户不但可以将String.int等字面值注入Bean中,还可以将集合.Map等类型注入Bean中,此外还可以注入配置文件中其他定义的Bean. 一.字面值 ...
- Spring学习(二)--装配Bean
一.Spring装配机制 Spring提供了三种主要的装配机制: 1.在XML中进行显示配置 2.在Java中进行显示配置 3.隐式的bean发现机制和自动装配--自动化装配bean Spring可以 ...
- Spring学习二:Spring Bean 定义
Bean 定义 被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的.bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象.这些 bean 是 ...
- Spring学习(二):Spring支持的5种Bean Scope
序言 Scope是定义Spring如何创建bean的实例的.Spring容器最初提供了两种bean的scope类型:singleton和prototype,但发布2.0以后,又引入了另外三种scope ...
- Spring学习(5)---Bean的定义及作用域的注解实现
Bean管理的注解实现 Classpath扫描与组件管理 类的自动检测与注册Bean <context:annotation-config/> @Component,@Repository ...
随机推荐
- 闪回之 回收站、Flashback Drop (table、index、trigger等)
一: Flashback Drop 操作流程 模式一:drop table 后未新建同名表 SQL> create table flashdrop as select * from user_o ...
- Java程序打包成exe可执行文件
前言: 我们都知道Java可以将二进制程序打包成可执行jar文件,双击这个jar和双击exe效果是一样一样的,但感觉还是不同.其实将java程序打包成exe也需要这个可执行jar文件. 准备: ecl ...
- java流类
总结:new FileInputStream package com.ds; import java.io.*; import com.da.fgbv; public class rter { pub ...
- UML核心元素--用例
定义:用例定义了一组用例实例,其中每个实例都是系统所执行的一些列操作,这些操作生成特定主角可以观测的值.一个完整的用例定义由参与者.前置条件.场景.后置条件构成. 1.理解用例:用例就是参与者希望通过 ...
- nodejs调用delphi编写的dll
公司的业务需要,nodejs要读取文件版本号. 同事要求我用delphi编写dll,以供nodejs调用,结果通过json返回. delphi代码如下: function GetFileInfo(AP ...
- C语言学习笔记--C语言中的逗号表达式
逗号表达式:exp1,exp2,epx3,...,expN; (1)逗号表达式是 C 语言中的“粘贴剂” (2)逗号表达式用于将多个子表达式连接为一个表达式 (3)逗号表达式的值为最后一个子表达式的值 ...
- ansible一键部署LAMP
一.实现ansible跟节点间无密码访问,不会配置的请看 文章 . 二.创建目录 $ mkdir -p playbooks/{files,templates} 三.创建php测试文件index.p ...
- [原创]SQL表值函数:返回从当前周开始往回的自定义周数
一如往常一样,一篇简短博文记录开发过程中遇到的一个问题.初衷都是记录自己的一些Idea,也是希望能够帮助一些凑巧遇到此类需求的问题,这个需求的的开端是因为,要统计最近N周的销售数据. 接下来我们来看看 ...
- 怀旧系列(3)----Pascal
Pascal语言是高中时代2000年左右为了参加计算机竞赛学习的一门语言(Turbo Pascal7.0).据说这么语言的结构化非常好,非常适合青少年形成一定的编程思想.但是现在的角度想想都是扯淡,现 ...
- Entity Framework Code-First(5):Code First Conventions
Code First Conventions: We have seen how EF Code-First creates DB tables from domain classes in the ...