Spring入门

 * spring介绍
 * 			Spring是一个从实际开发中抽取出来的框架,因此它完成了大量开发中的通用步骤,留给开发者的仅仅是与
 * 		特定应相关的部分,从而大大提高了企业应用的开发效率
 * 			Spring 为企业应用的开发提供了一个轻量级的解决方案,包括:基于依赖注入的核心机制,基于AOP的声明式事务管理
 * 		、与多种持久层技术的整合,可以说 Spring是企业应用开发的“一站式”选择,Spring贯穿表现层、业务层、持久层
 * 		总结Spring优点:
 * 			1.低耦合设计,对象依赖低
 * 			2.独立于各种应用服务器
 * 			3.Spring IoC容器降低了业务对象替换的复杂性,提高了组件之间的解耦
 * 			4.Spring AOP支持一些通用任务如:事务/日志进行集中处理,从而提供了更好的复用
 * 搭建环境:
 * 			spring-framework-4.2.4.RELEASE-dist.zip Spring开发环境包
 * 			解压:
 * 				docs:文档规范(包含开发指南、API参考文档)
 * 				libs:a.Spring框架class文件的JAR包 b.Spring 框架源文件的压缩包 c.API文档压缩包
 * 			schema:xml 约束文档
 * 			除此之外还得加一个日志包:commom-logging

创建一个普通类

public class Per {
	private Axe axe;


	public void setAxe(Axe axe) {
		this.axe = axe;
	}
	
	public void useAxe(){
		System.out.println("我打算去交个朋友..");
		System.out.println(axe.chop());
	}
}
public class Axe {
	
	public String chop(){
		return "交到了...";
	}
}
src下的appliactionContext.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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- 属性配置
			singleton 单实例(默认)
			只要配置文件一加载,就会根据bean标签创建对象
			注意:只会创建一个对象 放到spring容器中 当所有人来访问spring容器的时候,给的都是同一个对象
			prototype 多实例
			配置文件加载,不会根据bean标签创建对象
			当每次访问,就要创建一次对象放到spring容器中
	 -->
	<bean id="person" class="xinfei.code.bean.Per">
		<!-- 控制调用setAxe()方法,将容器中的axe Bean作为传入参数 -->
		<property name="axe" ref="axe"></property>
	</bean>
	<bean id="axe" class="xinfei.code.bean.Axe"></bean>
</beans>
TestJunit测试类

public class TestDemo {
	
	@Test
	public void test01(){
		User user = new User();
	}
	
	@Test
	public void test02(){
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		User user1 = (User)context.getBean("user");
		System.out.println(user1);
		user1.eat();
		User user2 = (User)context.getBean("user");
		System.out.println(user2);
		user2.eat();
	}
	
	@Test
	public void test03(){
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		User user = (User)context.getBean("user");
		user.eat();
		context.close();
	}
	
	@Test
	public void test04(){
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person = (Person)context.getBean("person");
		person.abcd();
		System.out.println(person);
	}
}
使用DI的方式

定义一个接口

public interface Person {
	
	public void abcd();
}
定义一个实现类

public class PersonImpl implements Person {

	public PersonImpl() {
		super();
	}

	private String name;
	private Integer age;
	private Car car;

	public void abcd() {
		System.out.println(name + "---" + age);
	}

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

	public void setAge(Integer age) {
		this.age = age;
	}

	public void setCar(Car car) {
		this.car = car;
	}

	@Override
	public String toString() {
		return "PersonImpl [name=" + name + ", age=" + age + ", car=" + car + "]";
	}
}
定义一个测试类

@Test
	public void test04(){
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person = (Person)context.getBean("person");
		person.abcd();
		System.out.println(person);
	}
	
	@Test
	public void test05(){
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Car car = (Car)context.getBean("car");
		car.run();
	}

复杂类型

public class CollBean {

	private String[] ss;
	private List ll;
	private Map mm;
	private Properties pp;

	public void setSs(String[] ss) {
		this.ss = ss;
	}

	public void setLl(List ll) {
		this.ll = ll;
	}

	public void setMm(Map mm) {
		this.mm = mm;
	}

	public void setPp(Properties pp) {
		this.pp = pp;
	}

	@Override
	public String toString() {
		return "CollBean [ss=" + Arrays.toString(ss) + ", ll=" + ll + ", mm=" + mm + ", pp=" + pp + "]";
	}
}
<?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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="collbean" class="xinfei.code.bean.CollBean">
		<property name="ss">
			<list>
				<value>aaaaa</value>
				<value>bbbbb</value>
				<value>ccccc</value>
				<value>ddddd</value>
			</list>
		</property>
		<property name="mm">
			<map>
				<entry key="abc" value="123"></entry>
				<entry key="efg" value="8910"></entry>
			</map>
		</property>
		<property name="pp">
			<props>
				<prop key="username">root</prop>
				<prop key="userpassword">123456</prop>
			</props>
		</property>
	</bean>
</beans>
测试复杂类型

@Test
	public void test06(){
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		CollBean collbean = (CollBean)context.getBean("collbean");
		System.out.println(collbean);
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值