Spring基础入门

1. 首先要引入spring的jar包。
<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
				<version>${spring.version}</version>
			</dependency>

			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-core</artifactId>
				<version>${spring.version}</version>
			</dependency>

			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-beans</artifactId>
				<version>${spring.version}</version>
			</dependency>



			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-test</artifactId>
				<version>${spring.version}</version>
				<scope>test</scope>
			</dependency>

			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-orm</artifactId>
				<version>${spring.version}</version>
			</dependency>
			
				<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-aop</artifactId>
				<version>${spring.version}</version>
			</dependency>

			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-webmvc</artifactId>
				<version>${spring.version}</version>
			</dependency>



2.建立一个beans.xml文件, 这个文件配置了spring容器的基本信息。

<?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.xsd">

 <bean id="user" class="com.lj.model.User">
   	<property name="classmates">
    <list>
     <ref="classmate01"/>
     <ref="classmate02"/>  
</list>
</property>
   </bean>

<!--这里我们假如user中包含一个list对象, 对象中包含了Classmate这个对象-->

<bean id="classmate01" class="com.lj.model.Classmate">
<property name="liming"/>
</bean>

<bean id="classmate02" class="com.lj.model.Classmate">
<property name="zhangdali"/>
</bean>



</beans>



3. 在test类中通过spring的ClassPathXmlApplicationContext("beans.xml");
来获取工厂。
public class SpringTest
{
	private BeanFactory factory=new ClassPathXmlApplicationContext("beans.xml");
	
	@Test
	public void testUserReader(){
		User u=factory.getBean("user", User.class);
		System.out.println(u);
	
	}
}

	



           

你可能感兴趣的:(spring,xml)