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

Spring简单实用bean配置

2016-12-31 00:00 405 查看
摘要: Spring简单实用bean配置

1.applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"> <!-- 配置bean class:bean的全类名 通过反射的方式在IOC容器中创建bean 所以要求Bean 中必须要有无参数的构造器 -->
<!-- 通过属性注入方式 比较常用 -->
<bean id="hello" class="com.huangliusong.spring.entity.HelloWorld">
<property name="name" value="huangliusong"></property>
</bean>

<!-- 使用构造器注入 -->
<bean id="car" class="com.huangliusong.spring.entity.Car">
<constructor-arg value="奥迪" type="java.lang.String"></constructor-arg>
<constructor-arg type="java.lang.String">
<!-- 如果字面值带特殊符号的值 可以用<![CDATA[]]>包含起来 -->
<value><![CDATA[<上海>]]></value>
</constructor-arg>
<constructor-arg value="1100000" type=""></constructor-arg>
</bean>

<!-- <bean id="person" class="com.huangliusong.spring.entity.Person"> <property
name="name" value="黄柳淞"></property> <property name="age" value="24"></property>
指向上面的bean id=car的bean 可以用property的ref属性建立bean之间的引用关系 这是引用外部bean <property
name="car" ref="car"></property> </bean> -->
<bean id="person" class="com.huangliusong.spring.entity.Person">
<property name="name" value="黄柳淞"></property>
<property name="age" value="24"></property>
<!-- 内部bean 不能为外部引用 -->
<property name="car">
<bean class="com.huangliusong.spring.entity.Car">
<constructor-arg value="福特"></constructor-arg>
<constructor-arg value="长安"></constructor-arg>
<constructor-arg value="20000"></constructor-arg>
</bean>
</property>
</bean>

<bean id="person2" class="com.huangliusong.spring.entity.Person">
<constructor-arg value="黄柳淞"></constructor-arg>
<constructor-arg value="23"></constructor-arg>
<constructor-arg ref="car"></constructor-arg>
<!-- 为级联属性赋值 -->
<property name="car.maxSpeed" value="12000"></property>
<!-- <constructor-arg><null/></constructor-arg> -->
</bean>

<!-- 测试如何配置集合属性 -->
<bean id="person3" class="com.huangliusong.spring.entity.colection.Person">
<property name="name" value="麦克"></property>
<property name="age" value="22"></property>
<property name="cars">
<list>
<ref bean="car1" />
<ref bean="car1" />
<ref bean="car1" />
</list>
</property>
</bean>
<!-- 使用构造器注入 -->
<bean id="car1" class="com.huangliusong.spring.entity.colection.Car">
<constructor-arg value="奥迪1" type="java.lang.String"></constructor-arg>
<constructor-arg type="java.lang.String">
<!-- 如果字面值带特殊符号的值 可以用<![CDATA[]]>包含起来 -->
<value><![CDATA[<上海1>]]></value>
</constructor-arg>
<constructor-arg value="110000110"></constructor-arg>
</bean>

<!-- 配置map集合属性值 -->
<bean id="person4" class="com.huangliusong.spring.entity.colection.NewPerson">
<property name="name" value="huangliusong"></property>
<property name="age" value="22"></property>
<property name="cars">
<map>
<entry key="aa" value-ref="car1">
</entry>
</map>
</property>
</bean>

<!-- 配置Properties属性值 -->
<bean id="dataSouece" class="com.huangliusong.spring.entity.colection.DataSource">
<property name="properties">
<!-- 使用props和prop为Properties属性赋值 -->
<props>
<prop key="user">root</prop>
<prop key="password">123</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>

<!-- 配置单例的集合bean 以提多个bean进行引用 需要导入util:list命名空间 -->
<!-- <util:list id="carsss"> <ref bean="car"/> </util:list> <bean id="person5"
class="com.huangliusong.spring.entity.colection.Person"> <property name="name"
value="小五"></property> <property name="age" value="23"></property> <property
name="cars" ref="carsss"></property> </bean> -->
<util:list id="cars">
<ref bean="car1" />
</util:list>
<!-- 通过命名空间对bean的属性进行赋值 需要先导入 p命名空间 -->
<bean id="person5" class="com.huangliusong.spring.entity.colection.Person"
p:age="30" p:name="黄柳淞p" p:cars-ref="cars">

</bean>
<bean id="person6"
class="com.huangliusong.spring.entity.colection.Person"> <property name="name"
value="小五"></property> <property name="age" value="23"></property> <property
name="cars" ref="cars"></property> </bean>
</beans>

2.Person.java

package com.huangliusong.spring.entity;

public class Person {
private String name;
private int age;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, int age, Car car) {
super();
this.name = name;
this.age = age;
this.car = car;
}
private Car car;
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", car=" + car
+ ", getName()=" + getName() + ", getAge()=" + getAge()
+ ", getCar()=" + getCar() + ", getClass()=" + getClass()
+ ", hashCode()=" + hashCode() + ", toString()="
+ super.toString() + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
}

3.Car.java

package com.huangliusong.spring.entity;

public class Car {
private String brand;
private String corp;
private int price;
public Car(String brand, String corp, int price) {
super();
this.brand = brand;
this.corp = corp;
this.price = price;
}
public Car(String brand, String corp, int price, int maxSpeed) {
super();
this.brand = brand;
this.corp = corp;
this.price = price;
this.maxSpeed = maxSpeed;
}
public Car() {
}
@Override
public String toString() {
return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price
+ ", maxSpeed=" + maxSpeed + ", getBrand()=" + getBrand()
+ ", getCorp()=" + getCorp() + ", getPrice()=" + getPrice()
+ ", getMaxSpeed()=" + getMaxSpeed() + ", getClass()="
+ getClass() + ", hashCode()=" + hashCode() + ", toString()="
+ super.toString() + "]";
}
private int maxSpeed;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getCorp() {
return corp;
}
public void setCorp(String corp) {
this.corp = corp;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
}

4.TestSpring.java

package com.huangliusong.spring.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.huangliusong.spring.entity.Car;
import com.huangliusong.spring.entity.HelloWorld;
import com.huangliusong.spring.entity.Person;

public class TestSpring {
//在没有spring下的普通调用
@Test
public void test1() {
HelloWorld h = new HelloWorld();
h.setName("huangliusong");
h.hello();
}
/**
* 1.创建spring的ioc容器对象
* applicationcontext代表IOC容器 该实现类从类路径下加载配置文件
*/
@Test
public void test2(){
//创建spring的IOC容器对象
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从IOC容器中获取bean的实例
HelloWorld helloWorld=(HelloWorld) ctx.getBean("hello");
//调用hello方法
helloWorld.hello();
}
@Test
public void test3(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Car car=(Car) ctx.getBean("car");
System.out.println(car);
}
@Test
public void test4(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Person p=(Person) ctx.getBean("person");
System.err.println(p);
}
}

5.TestSpring1.java

package com.huangliusong.spring.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.huangliusong.spring.entity.colection.DataSource;
import com.huangliusong.spring.entity.colection.NewPerson;
import com.huangliusong.spring.entity.colection.Person;

public class TestSpring1 {
@Test
public void test1(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Person person=(Person) ctx.getBean("person3");
System.out.println(person);
}
@Test
public void test2(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
NewPerson person=(NewPerson) ctx.getBean("person4");
System.out.println(person);
}
@Test
public void test3(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource=(DataSource) ctx.getBean("dataSouece");
System.out.println(dataSource.getProperties());
}
@Test
public void test4(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Person person=(Person) ctx.getBean("person5");
System.out.println(person);
}
@Test
public void test5(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Person person=(Person) ctx.getBean("person6");
System.out.println(person);
}

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