关闭

javaee之Spring的练习

标签: java eespring
690人阅读 评论(0) 收藏 举报
分类:
Spring:
Spring是一个javaee企业级应用的一站式框架,基于IOC和AOP思想的框架,可以整合Struts2、hibernate、Servlet、JDBC等技术和框架


Spring大致的体系分为6各模块:



IOC:
ioc,控制反转,把创建对象的行为交给容器去完成,是解决对象的创建问题;





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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        ">


	


	<!-- 调用的是无参的方法 -->
	 <bean id="s1" class="demo3_ioc.Student"></bean>
	
	 <!-- 调用的是有参的方法 -->
	<bean id="s2" class="demo3_ioc.Student">
		<constructor-arg index="0" value="暴龙">
		</constructor-arg>
		<constructor-arg index="1" value="12">
		</constructor-arg>
	</bean>
	
	<!-- 1、使用的工厂是成员方法 -->
	<bean id="sf" class="demo3_ioc.StudentFactory"></bean>
	<!-- 2、在调用方法 -->
	<bean id="s3"  factory-bean="sf" factory-method="getStudent"></bean>
	
	<!-- 调用静态方法 -->
	<bean class="demo3_ioc.StudentFactory" id="s4" factory-method="getStudentStatic"></bean>
	
	<!-- 单例和多例 
		singleton:默认的,为单例模式,即每一个bean都为同一个
		prototype:多例模式,即每一个bean都是新的
		lazy-init:为延迟创建bean
	-->
	<bean id="s5" class="demo3_ioc.Student" scope="prototype" lazy-init="true" ></bean>
</beans>

Student:

public class Student {


	//使用无参的构造方法,默认的控制反转为无参
	public Student(){
		System.out.println("调用了无参");
	}
	
	//使用有参的构造方法来反转
	public Student(String name,int age){
		System.out.println("调用了有参:name = "+name+ ";age = "+age);
	}
}


StudentFactory:
public class StudentFactory {


	//调用成员方法来ioc
	public Student getStudent(){
		System.out.println("成员方法");
		return new Student();
	}
	
	//调用静态方法来ioc
	public static Student getStudentStatic(){
		System.out.println("静态方法");
		return new Student();
	}
}


测试类:

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


public class Demo {


	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("demo3_ioc/applicationContext.xml");  //读取的文件是在类路径下的文件
		Student s  = (Student)ac.getBean("s2");
		/*for(int i=1;i<=5;i++){
			Student s  = (Student)ac.getBean("s2");
			System.out.println(s);
		}*/
	}
}

在ioc的过程中,需要注意的是:
1)、不用考虑线程安全的情况尽量使用单例,用以节省内存,如在dao层、service层
2)、如果有线程安全的情况下必须使用多例,如action对象的创建


DI
,依赖注入,把对象之间的关系交给容器完成,是解决对象与对象之间的依赖关系


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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        ">


	<bean id="ul" class="demo4_di.User">
		<property name="name" value="暴龙"></property>
		<property name="age" value="12"></property>
		
		<!-- 数组的注入 -->
		<property name="addresses">
			<array>
				<value>广州高铁</value>
				<value>广西高铁</value>
			</array>
		</property>
		
		<!-- List集合的注入 -->
		<property name="addressList">
			<list>
				<value>珠海常量</value>
				<value>广州常量</value>
			</list>
		</property>
		
		<!-- Map集合的注入 -->
		<property name="addressMap">
			<map>
				<entry key="gd" value="广东"></entry>
				<entry key="gx" value="广西"></entry>
			</map>
		</property>
		
		<!-- Properties对象的注入 -->
		<property name="addressProp">
			<props>
				<prop key="gd">广东</prop>
				<prop key="gx">广西</prop>
			</props>
		</property>
		
		<!-- javabean对象的注入 -->
		<property name="addr" ref="address1">
		</property>
		
		<!-- List和javabean的注入 -->
		<property name="addressList2">
			<list>
				<ref bean="address1"/>
				<ref bean="address2"/>
			</list>
		</property>
		
	</bean>
	
	<!-- Address的注入 -->
	<bean id="address1" class="demo4_di.Address">
		<property name="name" value="广州"></property>
	</bean>
	<bean id="address2" class="demo4_di.Address">
		<property name="name" value="珠海"></property>
	</bean>
	
</beans>

User:
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;


public class User {


	//1.基本数据类型:String
		private String name;
		//用于外部注入数据
		public void setName(String name) {
			this.name = name;
		}
		private int age;
		public void setAge(int age) {
			this.age = age;
		}
		
		//2.数组类型
		private String[] addresses;
		public void setAddresses(String[] addresses) {
			this.addresses = addresses;
		}
		
		//3.List集合
		private List<String> addressList;
		public void setAddressList(List<String> addressList) {
			this.addressList = addressList;
		}
		
		
		//4.Map集合
		private Map<String,String> addressMap;
		public void setAddressMap(Map<String, String> addressMap) {
			this.addressMap = addressMap;
		}
		
		
		//5.Properties对象
		private Properties addressProp;
		public void setAddressProp(Properties addressProp) {
			this.addressProp = addressProp;
		}
		
		//6.javabean对象
		private Address addr;
		public void setAddr(Address addr) {
			this.addr = addr;
		}
		
		
		//7: List集合+javabean
		private List<Address> addressList2;
		public void setAddressList2(List<Address> addressList2) {
			this.addressList2 = addressList2;
		}
		@Override
		public String toString() {
			return "User [name=" + name + ", age=" + age + ", addresses="
					+ Arrays.toString(addresses) + ", addressList=" + addressList
					+ ", addressMap=" + addressMap + ", addressProp=" + addressProp
					+ ", addr=" + addr + ", addressList2=" + addressList2 + "]";
		}
}

Address:
	
public class Address {


	private String name;


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	@Override
	public String toString() {
		return "Address [name=" + name + "]";
	}
	
	
}

测试类:


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


public class Demo {


	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("demo4_di/applicationContext.xml");
		User u = (User)ac.getBean("ul");
	
		System.out.println(u);
	}
}


Spring的注解:

IOC注解:
@Component: 用在类的顶部,代表声明一个对象,
@Respositoty:等价于@component,用于持久层(dao层)
@Service:等价于@component,用于业务层(service层)
@Controller:等价于@component,用于视图层(web层)


DI注解:
@Resource:用于注入数据对象,
@Autowire:用于注入数据,自定义匹配名称进行注入(自动的注入)


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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        ">
      
      <!-- 自动扫描在项目中带有注解的类 -->
      <context:component-scan base-package="demo5_annotation"></context:component-scan>
	
</beans>



User:
import javax.annotation.Resource;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;


//@Component("userID")
@Repository("userID")  //repository和component的效果一样,都是在类上进行反转,如<bean id="userID" />
public class User {


	@Resource(name="addID")
	//如果属性名称和需要注入的javabean的ID名称一致,可以使用@Autowise(可以省略setter方法)
	@Autowired //自动装载
	private Address add;
	
	public void setAdd(Address add) {
		this.add = add;
	}


	public User(){
		System.out.println("构造");
	}


	@Override
	public String toString() {
		return "User [add=" + add + "]";
	}
	
	
}

Address:
import org.springframework.stereotype.Component;


@Component("addID")
public class Address {


	public Address(){
		System.out.println("调用Address构造方法");
	}
}


测试类:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Demo {


	public static void main(String[] args) {
			ApplicationContext ac = new ClassPathXmlApplicationContext("demo5_annotation/applicationContext.xml");
			
			User u = (User)ac.getBean("userID");
			System.out.println(u);
		}
	}

在Spring中,主要要了解的是IOC和AOP,而IOC是基础底层,解决对象的问题,大大增加开发效率,不过也是会减低运行效率。
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:36579次
    • 积分:1266
    • 等级:
    • 排名:千里之外
    • 原创:92篇
    • 转载:1篇
    • 译文:0篇
    • 评论:15条
    文章分类
    最新评论