Spring学习随笔-1

本部分为Spring的入门知识

包含如下部分内容

<!--1.属性注入 -->

<!--2.构造器注入-->

<!--3.使用<![CDATA[]]>转译特殊字符-->

<!--4.构造器+属性+Bean对象注入-->

<!--5.内部Bean,不能被外部引用-->

<!--6.null设定-->

<!--7.级联属性-->

<!--8.集合属性-->

<!--9.配置单独的集合Bean -->

<!--10.使用p配置属性-->

 

具体代码如下

package com.liu.bean

 

Company

 

package com.liu.bean;

public class Company {
	private Person person;
	private String name;
	
	public Company(Person person) {
		super();
		this.person = person;
	}
	
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Company [member=" + person + ", name=" + name + "]";
	}
	
	public void setPerson(Person person) {
		this.person = person;
	}
	
	public Person getPerson() {
		return person;
	}
}


HelloSpring

package com.liu.bean;

public class HelloSpring {

	private String name;
	
	public void sayHello(){
		System.out.println("hello:"+name);
	}
	
	public void setName(String name) {
		this.name = name;
	}
}


 

Person

package com.liu.bean;

public class Person {
	private String name;
	private String sex;
	private int age;
	private double sale;
	
	public Person(String name, String sex,int age) {
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	public Person(String name, String sex, double sale) {
		this.name = name;
		this.sex = sex;
		this.sale = sale;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", sex=" + sex + ", age=" + age + ", sale=" + sale + "]";
	}
	
	public void setSale(double sale) {
		this.sale = sale;
	}
	
	public double getSale() {
		return sale;
	}
}


 

 

com.liu.collections

Company

package com.liu.collections;

import java.util.List;
import java.util.Map;

import com.liu.bean.Person;

public class Company {
	private String name;
	private List<Person> persons;
	private Map<String,Person> members;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<Person> getPersons() {
		return persons;
	}
	public void setPersons(List<Person> persons) {
		this.persons = persons;
	}
	
	public void setMembers(Map<String, Person> members) {
		this.members = members;
	}
	
	@Override
	public String toString() {
		return "Company [name=" + name + ", persons=" + persons + ", members=" + members + "]";
	}
	
	
	
}


com.liu.test

 

Test

package com.liu.test;

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

import com.liu.bean.*;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ApplicationContext ctx =  new ClassPathXmlApplicationContext("applicationContext.xml");
		HelloSpring helloSpring = (HelloSpring)ctx.getBean("helloSpring");
		helloSpring.sayHello();
		Person person1 = (Person)ctx.getBean("person1");
		Person person2 = (Person)ctx.getBean("person2",Person.class);
		System.out.println(person1.toString());
		System.out.println(person2.toString());
		
		Company company= (Company)ctx.getBean("company");
		System.out.println(company.toString());
		
		Company company2= (Company)ctx.getBean("company2");
		System.out.println(company2.toString());
		
		Company company3= (Company)ctx.getBean("company3");
		System.out.println(company3.toString());
		
		Company company4= (Company)ctx.getBean("company4");
		System.out.println(company4.toString());
		
		com.liu.collections.Company company5 = (com.liu.collections.Company)ctx.getBean("company5");
		System.out.println(company5.toString());
		
		com.liu.collections.Company company6 = (com.liu.collections.Company)ctx.getBean("company6");
		System.out.println(company6.toString());
		
		com.liu.collections.Company company7 = (com.liu.collections.Company)ctx.getBean("company7");
		System.out.println(company7.toString());
	}

}


 

配置文件 applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd  
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd  
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
    
    <!--1.属性注入 -->
	<bean id="helloSpring" class="com.liu.bean.HelloSpring">
		<property name="name" value="Spring"></property>
	</bean>
	
	<!--2.构造器注入-->
	<bean id="person1" class="com.liu.bean.Person">
		<constructor-arg value="liu"></constructor-arg>
		<constructor-arg value="男"></constructor-arg>
		<constructor-arg value="18" type="int" ></constructor-arg>
	</bean>
	
	<bean id="person2" class="com.liu.bean.Person">
		<!--3.使用<![CDATA[]]>转译特殊字符-->
		<constructor-arg ><value><![CDATA[<liu>]]></value></constructor-arg>
		<constructor-arg value="男"></constructor-arg>
		<constructor-arg value="18" type="double"></constructor-arg>
	</bean>
	
	<!--4.构造器+属性+Bean对象注入-->
	<bean id="company" class="com.liu.bean.Company">
		<property name="name" value="上海家华"></property>
		<constructor-arg ref="person1"></constructor-arg>
	</bean>
	
	<!--5.内部Bean,不能被外部引用-->
	<bean id="company2" class="com.liu.bean.Company">
		<property name="name" value="上海日化"></property>
		<constructor-arg >
			<bean id="person2" class="com.liu.bean.Person">
				<constructor-arg value="liu2"></constructor-arg>
				<constructor-arg value="男"></constructor-arg>
				<constructor-arg value="18" type="int" ></constructor-arg>
			</bean>		
		</constructor-arg>
	</bean>
	
	<!--6.null设定-->
	<bean id="company3" class="com.liu.bean.Company">
		<property name="name" value="上海家华"></property>
		<constructor-arg><null/></constructor-arg>
	</bean>
	
	<!--7.级联属性-->
	<bean id="company4" class="com.liu.bean.Company">
		<constructor-arg ref="person1"></constructor-arg>
		<property name="person.sale" value="1000"></property>
	</bean>
	
	<!--8.集合属性-->
	<bean id="company5" class="com.liu.collections.Company">
		<property name="name" value="新公司"></property>
		<property name="persons">
			<list>
				<ref bean="person1" />
				<ref bean="person2" />
			</list>
		</property>
		
		<property name="members">
			<map>
				<entry key="p1" value-ref="person1" />
				<entry key="p2" value-ref="person2" />
			</map>
		</property>
	</bean>
	
	<!--9.配置单独的集合Bean -->
	<util:list id="carList">
		<ref bean="person1" />
		<ref bean="person2" />
	</util:list>
	
	<bean id="company6" class="com.liu.collections.Company">
		<property name="name" value="新公司"></property>
		<property name="persons" ref="carList">
		</property>
	</bean>
	
	<!--10.使用p配置属性-->
	<bean id="company7" class="com.liu.collections.Company" p:name="公司7" p:persons-ref="carList">
	</bean>
 </beans>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值