Spring框架(二)

Spring反射机制:

1, 通过spring来获取一个对象的实例

1     <bean id="user" class="com.model.User">
2
3     </bean>
1         ApplicationContext ac=new ClassPathXmlApplicationContext("Spring-all.xml");//调用此方法时类已经实例化好了
2         User u=(User)ac.getBean("user");//拿到实例化的类

2, 通过spring进行属性注入
  setter方法注入

1     <!-- 通过Spring給类注入属性,通过空参构造方法 -->
2         <property name="username">
3             <value>hanqi</value>
4         </property>
5         <property name="password">
6             <value>123</value>
7         </property>   

  构造器注入

1         <!--
2         另一种通过带参构造方法-->
3         <constructor-arg index="0" value="name1"></constructor-arg>
4         <constructor-arg index="1" value="pwd1"></constructor-arg>
5         

  接口注入

1 public class ClassA {
2   private InterfaceB clzB;
3   public init() {
4   Ojbect obj =
5   Class.forName(Config.BImplementation).newInstance();
6   clzB = (InterfaceB)obj;
7   }
8   ……
9   } 

  上面的代码中,ClassA依赖于InterfaceB的实现,如何获得InterfaceB实现类的实例?传统的方法是在代码中创建InterfaceB实现类的实例,并将起赋予clzB。

  而这样一来,ClassA在编译期即依赖于InterfaceB的实现。为了将调用者与实现者在编译期分离,于是有了上面的代码,我们根据预先在配置文件中设定的实现类的类名,动态加载实现类,并通过InterfaceB强制转型后为ClassA所用。
  这就是接口注入的一个最原始的雏形。
  而对于一个Type1型IOC容器而言,加载接口实现并创建其实例的工作由容器完成,如J2EE开发中常用的Context.lookup(ServletContext.getXXX),都是Type1型IOC的表现形式。
  Apache Avalon是一个典型的Type1型IOC容器。

  p标记的使用
  <bean p:username=""></bean>

1     <bean id="user" class="com.model.User" p:username="pusername" p:password="ppwd">
2     </bean>

3, 将一个对象注入到另一个对象<ref bean="...">

用户有一个部门

部门有多个用户

model:

 1 package com.model;
 2
 3 public class User {
 4     private String username;
 5     private String password;
 6     private Dept dept;
 7
 8
 9     public User() {
10         super();
11         // TODO Auto-generated constructor stub
12     }
13     public User(String username, String password, Dept dept) {
14         super();
15         this.username = username;
16         this.password = password;
17         this.dept = dept;
18     }
19     public String getUsername() {
20         return username;
21     }
22     public void setUsername(String username) {
23         this.username = username;
24     }
25     public String getPassword() {
26         return password;
27     }
28     public void setPassword(String password) {
29         this.password = password;
30     }
31     public Dept getDept() {
32         return dept;
33     }
34     public void setDept(Dept dept) {
35         this.dept = dept;
36     }
37     @Override
38     public String toString() {
39         return "User [username=" + username + ", password=" + password + ", dept=" + dept + "]";
40     }
41
42
43 }
 1 package com.model;
 2
 3 import java.util.List;
 4
 5 public class Dept {
 6     private int deptid;
 7     private String deptname;
 8     private List<User> users;
 9
10     public Dept() {
11         super();
12         // TODO Auto-generated constructor stub
13     }
14
15     public Dept(int deptid, String deptname, List<User> users) {
16         super();
17         this.deptid = deptid;
18         this.deptname = deptname;
19         this.users = users;
20     }
21
22     public int getDeptid() {
23         return deptid;
24     }
25     public void setDeptid(int deptid) {
26         this.deptid = deptid;
27     }
28     public String getDeptname() {
29         return deptname;
30     }
31     public void setDeptname(String deptname) {
32         this.deptname = deptname;
33     }
34
35     public List<User> getUsers() {
36         return users;
37     }
38
39     public void setUsers(List<User> users) {
40         this.users = users;
41     }
42
43     @Override
44     public String toString() {
45         return "Dept [deptid=" + deptid + ", deptname=" + deptname + ", users人数=" + users.size() + "]";
46     }
47
48 }

配置文件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 6
 7     <bean id="user1" class="com.model.User">
 8         <property name="username" value="苦海"></property>
 9         <property name="password" value="111"></property>
10         <property name="dept" ref="dept1"></property>
11
12     </bean>
13
14     <bean id="dept1" class="com.model.Dept">
15         <property name="deptid" value="6001"></property>
16         <property name="deptname" value="部门1"></property>
17         <property name="users">
18             <list>
19                 <ref bean="user1"/>
20             </list>
21         </property>
22     </bean>
23
24 </beans>

测试:

 1 package com.test;
 2
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5
 6 import com.model.Dept;
 7 import com.model.User;
 8
 9 public class Test {
10     public static void main(String[] args) {
11         ApplicationContext ac=new ClassPathXmlApplicationContext("Spring-all.xml");//调用此方法时类已经实例化好了
12
13         User u=(User)ac.getBean("user1");//拿到实例化的类
14         Dept d=(Dept)ac.getBean("dept1");
15
16         System.out.println(u);
17         System.out.println(d);
18         ((ClassPathXmlApplicationContext)ac).close();
19
20     }
21 }

4, AutoWired(byType, byName)

  autowire

自动装载:

byName根据名字自动注入

user1的bean中并没有dept属性,但是还是打印出了这个属性,因为它会找到这个类,然后在配置文件中找到和该属性同名的id,并自动注入

 1     <bean id="user1" class="com.model.User" autowire="byName">
 2         <property name="username" value="苦海"></property>
 3         <property name="password" value="111"></property>
 4     </bean>
 5
 6     <bean id="dept" lazy-init="true" class="com.model.Dept" init-method="init" destroy-method="destory"  >
 7         <property name="deptid" value="6001"></property>
 8         <property name="deptname" value="部门1"></property>
 9         <property name="users">
10             <list>
11                 <ref bean="user1"/>
12             </list>
13         </property>
14     </bean>
1         ApplicationContext ac=new ClassPathXmlApplicationContext("Spring-all.xml");//调用此方法时类已经实例化好了
2         User u=(User)ac.getBean("user1");//拿到实例化的类
3         System.out.println(u);
4         ((ClassPathXmlApplicationContext)ac).close();

byType根据类型自动装载,用法一致

需要注意,如果根据类型自动装载,应只有一个该类型,否则会无发找到,报错

autowire默认default,指的是根据<beans>声明中得来选择方法

5, scope, lazy-init, init-method, destroy-method(相当的不重要)
  scope="singleton(单例) / prototype(原型)"

默认情况下Spring中定义的Bean是以单例模式创建的。
在GoF中的单例模式是指一个ClassLoader中只存在类一个实例。
而在Spring中的单例实际上更确切的说应该是:
1.每个Spring Container中定义的Bean只存在一个实例
2.每个Bean定义只存在一个实例。

1     <bean id="user1" class="com.model.User" scope="singleton">
2         <property name="username" value="苦海"></property>
3         <property name="password" value="111"></property>
4     </bean>
1         ApplicationContext ac=new ClassPathXmlApplicationContext("Spring-all.xml");//调用此方法时类已经实例化好了
2         User u=(User)ac.getBean("user1");//拿到实例化的类
3         User u2=(User)ac.getBean("user1");
4         System.out.println(u==u2);
5         ((ClassPathXmlApplicationContext)ac).close();

1     <bean id="user1" class="com.model.User" scope="prototype">
2         <property name="username" value="苦海"></property>
3         <property name="password" value="111"></property>
4     </bean>
1         ApplicationContext ac=new ClassPathXmlApplicationContext("Spring-all.xml");
2         User u=(User)ac.getBean("user1");
3         User u2=(User)ac.getBean("user1");
4         System.out.println(u==u2);
5         ((ClassPathXmlApplicationContext)ac).close();

  lazy-init="true" // 延迟加载,未生效

1 <bean id="dept1" lazy-init="true" class="com.model.Dept" init-method="init" destroy-method="destory"  >

写在beans中,设置全局延迟加载

1     default-lazy-init="true"
lazy-init    (一开始不初始化,用到的时候才初始化)
    init-method="init" destory-method="destory"    不要和prototype一起使用
类被初始化的时候调用init,被消亡的时候调用destory

正常运行的结果只有一个init和destroy,虽然两个service实例化,但是默认是单例,加了scope=prototype就运行不正常了,结果两个init,没有destroy,原因未知。 
首先我们应该知道:

一、spring Bean的作用域:scope=singleton(默认,单例,生成一个实例)

二、spring Bean的作用域:scope=prototype(多线程, 生成多个实例)

三、单例模式,默认在程序初始化的时候实例化(lazy-init=”false”)

四、prototype,getBean的时候才是实例化

五、lazy-init 只对单例模式起作用,对 prototype 不起作用(因为 prototype 默认就不是程序初始化的时候实例化的)

时间: 2024-08-07 20:35:57

Spring框架(二)的相关文章

Spring框架二

Bean管理 1.BeanFactory介绍:Spring容器,也称为Spring的上下文,是产生Bean的工厂,是Spring依赖注入的核心.      Bean:在Spring应用场景中,Bean可以使数据源,java普通类,还可以是hibernate框架的sessionFactory.事务组件等等 1.在Spring技术中是基于组件的. 2.最基本也是最常见的单元. 3.其实例保存在Spring容器当中 2.BeanFactory的作用: 1.配置.创建以及管理Bean对象 2.维持Bea

Spring框架初识(二)

1. AOP的相关概念       1.1 AOP概述         1.1.1 什么是AOP             AOP:全程是Aspect Oriented Programming 即面向切面编程.是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型.利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了

spring框架学习(二)依赖注入

转自:http://blog.csdn.net/lishuangzhe7047/article/details/20740835 ———————————————————————————————————————————— spring框架为我们提供了三种注入方式,分别是set注入,构造方法注入,接口注入.接口注入不作要求,下面介绍前两种方式. 1,set注入 采用属性的set方法进行初始化,就成为set注入. 1)给普通字符类型赋值. [java] view plaincopyprint? pub

[Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.

前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习. 一, AspectJ的概述: AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件. Spring为了简化自身的AOP的开发,将AspectJ拿过来作为Spring自身一个AOP的开发.

跟着刚哥学习Spring框架--Spring容器(二)

Spring容器 启动Spring容器(实例化容器) -- IOC容器读取Bean配置创建Bean实例之前,必须对它进行实例化(加载启动),这样才可以从容器中获取Bean的实例并使用.  Bean是Spring管理的基本单位,任何的Java对象和组件都被当成Bean处理,容器还负责管理Bean与Bean之间的依赖关系.  两种类型的启动实现   1.BeanFactory:IOC容器的基本实现,是Spring框架的基础设施,面向Spring本身: -- Spring容器最基本的接口就是BeanF

Spring框架学习之IOC(二)

Spring框架学习之IOC(二) 接着昨天的内容,下面开始IOC基于注解装配相关的内容 在 classpath 中扫描组件 <context:component-scan> 特定组件包括: –@Component: 基本注解, 标识了一个受 Spring 管理的组件 –@Respository: 标识持久层组件 –@Service: 标识服务层(业务层)组件 –@Controller: 标识表现层组件 对于扫描到的组件, Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写.

Spring框架深入(二)--AOP面向切面

一.AOP概述 1.AOP:面向切面编程,实现在不增加代码的基础上,增加一些新的功能(公共功能): 2.AOP并不是Spring框架持有的,Spring只是支持AOP编程的框架之一,可以整合第三方框架来实现面向切面编程(如:Aspect): 3.现实的应用场景:使用面向切面编程,AOP框架已经实现了面向切面的很多内容: 4.程序员使用AOP要做的事情: 编写公共功能,切面: 基于AOP框架的配置,直接把核心业务和切面关联起来: 5.Spring中实现AOP的方式有三种: 基于AspectJ注解的

spring框架学习笔记(二)

配置Bean Ioc容器 Ioc容器需要实例化以后才可以从Ioc容器里获取bean实例并使用. spring提供两种方式类型的Ioc容器实现: BeanFactory:底层的,面向spring框架的. ApplicationContext :面向开发人员的,一般用这个. 有两个实现类: ClassPathXmlApplicationContext:从类路径下加载配置文件. FileSystemXmlApplicationContext:从文件系统中加载配置文件. 两种方式配置文件是相同的. 通过

Spring框架学习(二)IOC注入之set/get方法

action层需要调用service层对象,service层又需要调用dao层的对象.一般情况而言就直接newXXX()使用就可以了,但 是这种方法会使程序的耦合性太强.当Action类中需要更换service方法时,就需要改动源代码.Spring框架就用IOC注入的方法帮我们解决 了这个问题. 示例代码(模拟WEB环境下): applicationContext.xml: <?xml version="1.0" encoding="UTF-8"?> &

Spring框架知识复习之二

Spring使用注解对Bean进行管理 1 使用注解需配置aop相关xsd文件的约束和命名空间 xsd文件名为:spring-aop-4.2.xsd 2 注解组件扫描配置 示例如下:base-package属性 设置扫描指定包下的所有子孙包 <context:component-scan base-package="cn.itma.bean"></context:component-scan> 3 spring管理bean的常用注解 <1>作用在类上