您的位置:首页 > 编程语言 > Java开发

spring入门基础

2012-03-10 10:09 239 查看
Spring入门—反射技术

无参数的

Java代码

Class.forName(className).newInstance;

//有参数的

Class.forName(className).getDeclaredConstructor(String.class).newInstance(“黎明”);

//通过反射获取属性

Introspector.getBeanInfo(Person.class).getPropertyDescriptors()

//通过反射机制修改bean属性的值

Person person=(Person)Class.forName(className).getDeclaredConstructor(String.class).newInstance("黎明");

PropertyDescriptor[] ps = Introspector.getBeanInfo(Person.class).getPropertyDescriptors();

for(PropertyDescriptor p :ps){

System.out.println(p.getName());

if(p.getName().equals("name")){

Method setter=p.getWriteMethod();

if(setter!=null){

setter.setAccessible(true);//允许访问private属性

setter.invoke(person, "小燕子");

//通过反射机制修改bean字段的值

Field field=Person.class.getDeclaredField("name");

field.setAccessible(true);//允许访问private字段

field.set( person , "sss");

Class.forName(className).newInstance;

//有参数的

Class.forName(className).getDeclaredConstructor(String.class).newInstance(“黎明”);

//通过反射获取属性

Introspector.getBeanInfo(Person.class).getPropertyDescriptors()

//通过反射机制修改bean属性的值

Person person=(Person)Class.forName(className).getDeclaredConstructor(String.class).newInstance("黎明");

PropertyDescriptor[] ps = Introspector.getBeanInfo(Person.class).getPropertyDescriptors();

for(PropertyDescriptor p :ps){

System.out.println(p.getName());

if(p.getName().equals("name")){

Method setter=p.getWriteMethod();

if(setter!=null){

setter.setAccessible(true);//允许访问private属性

setter.invoke(person, "小燕子");

//通过反射机制修改bean字段的值

Field field=Person.class.getDeclaredField("name");

field.setAccessible(true);//允许访问private字段

field.set( person , "sss");

Spring提供了声明式的事务管理

软件的解耦合,不是硬编码

Spring 需要的jar

Dist\spring.jar

lib\jakarta-commons\commons-logging.jar

如果使用了切面编程(AOP),还需要下列jar文件

lib/aspectj/aspectjweaver.jar和aspectjrt.jar

lib/cglib/cglib-nodep-2.1_3.jar

如果使用了JSR-250中的注解,如@Resource/@PostConstruct/@PreDestroy,还需要下列jar文件

lib\j2ee\common-annotations.jar

配置文件beans.xml

Java代码

<?xml version="1.0" encoding="UTF-8"?>

t;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-2.5.xsd">
<bean id="xx" class="junit.test.Person" lazy-init="true">

</bean>

t;/beans>

<?xml version="1.0" encoding="UTF-8"?>

<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-2.5.xsd">
<bean id="xx" class="junit.test.Person" lazy-init="true">

</bean>

</beans>

怎么启动spring容器Java代码

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

Person s = (Person)context.getBean("xx");

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

Person s = (Person)context.getBean("xx");

默认bean是容器启动时实例化,只在容器中创建一次,spring中的对象一直存在容器中,是单例模式

表 3.4. Bean作用域

作用域 描述
singleton 在每个Spring IoC容器中一个bean定义对应一个对象实例。
prototype 一个bean定义对应多个对象实例。
request 在一次HTTP请求中,一个bean定义对应一个实例;即每次HTTP请求将会有各自的bean实例, 它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext情形下有效。
session 在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。
global session 在一个全局的HTTP Session中,一个bean定义对应一个实例。典型情况下,仅在使用portlet context的时候有效。该作用域仅在基于web的Spring ApplicationContext情形下有效。

Spring入门—利用工厂方法创建bean

Java代码

public class PersonFactory {

public static Person createPerson(){

return new Person();

}

public Person createPerson2(){

return new Person();

}

public class PersonFactory {

public static Person createPerson(){

return new Person();

}

public Person createPerson2(){

return new Person();

}

}

Java代码

//使用静态工厂方法实例化

<bean id="person" class="bean.PersonFactory" factory-method="createPerson"></bean>

使用实例工厂方法实例化

<bean id="personFactory" class="bean.PersonFactory"></bean>

<bean id="person2" factory-bean="personFactory" factory-method="createPerson2"></bean>

//使用静态工厂方法实例化

<bean id="person" class="bean.PersonFactory" factory-method="createPerson"></bean>

使用实例工厂方法实例化

<bean id="personFactory" class="bean.PersonFactory"></bean>

<bean id="person2" factory-bean="personFactory" factory-method="createPerson2"></bean>

Spring入门—依赖注入

Java代码

public class Person {

private Integer id;

private String name="aa";

private IDCard idcard;

public IDCard getIdcard() {

return idcard;

}

public void setIdcard(IDCard idcard) {

this.idcard = idcard;

}

public Person(String name) {

this.name = name;

}

public Person() {

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

public class IDCard {

private String no;

public String getNo() {

return no;

}

public void setNo(String no) {

this.no = no;

}

}

public class Person {

private Integer id;

private String name="aa";

private IDCard idcard;

public IDCard getIdcard() {

return idcard;

}

public void setIdcard(IDCard idcard) {

this.idcard = idcard;

}

public Person(String name) {

this.name = name;

}

public Person() {

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

public class IDCard {

private String no;

public String getNo() {

return no;

}

public void setNo(String no) {

this.no = no;

}

}

第一种方法
Java代码

<bean id="xx" class="junit.test.Person" lazy-init="true">

<property name="idcard">

<bean class="junit.test.IDCard">

<property name="no" value="9999"></property>

</bean>

</property>

</bean>

<bean id="xx" class="junit.test.Person" lazy-init="true">

<property name="idcard">

<bean class="junit.test.IDCard">

<property name="no" value="9999"></property>

</bean>

</property>

</bean>

第二种方法

Java代码

<bean id="aa" class="junit.test.IDCard">

<property name="no" value="88888888"></property>

</bean>

<bean id="xx" class="junit.test.Person" lazy-init="true">

<property name="idcard" ref="aa">

</property>

</bean>

<bean id="aa" class="junit.test.IDCard">

<property name="no" value="88888888"></property>

</bean>

<bean id="xx" class="junit.test.Person" lazy-init="true">

<property name="idcard" ref="aa">

</property>

</bean>

为属性配置null值

Java代码

<property name="name"><null/></property>

public class Person {

private String name="ss";

public Person(){}

public Person(String name) {

this.name = name;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public void say(){

System.out.println("我说了");

}

}

<property name="name"><null/></property>

public class Person {

private String name="ss";

public Person(){}

public Person(String name) {

this.name = name;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public void say(){

System.out.println("我说了");

}

}

初始化bean执行say方法相当于测试单元的@BeforeClass

<bean id="xxx" class="bean.Person" scope="singleton" lazy-init="false" init-method="say">

集合依赖注入

Java代码

<property name="lists">

<list>

<value>1111</value>

<value>2222</value>

<value>3333</value>

<value>4444</value>

</list>

</property>

for(String s : p.getLists){

System.out.println(s);

}

<property name="sets">

<set>

<value>TTT</value>

<value>YYY</value>

</set>

</property>

for(String s : p.getSets){

System.out.println(s);

}

<property name="maps">

<map>

<entry key="key1" value="value1"></entry>

<entry key="key2" value="value2"></entry>

</map>

</property>

for(String key : p.getMaps().keySet()){

System.out.println(key+"="+ p. getMaps ().get(key));

}

Properties 是注入

<property name="propers">

<props>

<prop key="proper1">value1</prop>

<prop key="proper2">value2</prop>

</props>

</property>

for(Object key : p.getPropers().keySet()){

System.out.println(key+"="+ p.getPropers().get(key));

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: