Spring在eclipse里安装插件

第一步:打开eclipse,点开help------>Eclipse Marketplace

第二步:搜索spring

第三步:下载,Spring tools必须全部全选

第四步:安装


这里是spring里的一些配置、标签什么的,大家可以看一看。


在这里给大家举个例子

创建一个person的类

package com.zking.entity;

import java.util.List;

public class Person {
	private int pid;//编号
	private String pname;//姓名
	//对象集合
	private Card card;
	public Card getCard() {
		return card;
	}
	public void setCard(Card card) {
		this.card = card;
	}
	//字符串集合
	private List<String> hobbies;
	public List<String> getHobbies() {
		return hobbies;
	}
	public void setHobbies(List<String> hobbies) {
		this.hobbies = hobbies;
	}
	public Person() {
		super();
		System.out.println("person的构造方法");
	}
	public Person(int pid, String pname) {
		super();
		this.pid = pid;
		this.pname = pname;
	}
	public int getPid() {
		return pid;
	}
	public void setPid(int pid) {
		this.pid = pid;
	}
	public String getPname() {
		return pname;
	}
	public void setPname(String pname) {
		this.pname = pname;
	}
	@Override
	public String toString() {
		return "Person [pid=" + pid + ", pname=" + pname + "]";
	}
}

在创建一个Card的类

package com.zking.entity;

public class Card {
	private int cid;
	private String cname;
	public Card() {
		super();
		System.out.println("Card的构造方法");
	}
	public Card(int cid, String cname) {
		super();
		this.cid = cid;
		this.cname = cname;
	}
	public int getCid() {
		return cid;
	}
	public void setCid(int cid) {
		this.cid = cid;
	}
	public String getCname() {
		return cname;
	}
	public void setCname(String cname) {
		this.cname = cname;
	}
}

在写一个配置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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- 只要是个类,那么它就是bean -->
	<!-- 只要在配置里面配置过的启动都会被加载 -->
	<!-- 配置person对象  id放对象的名字-->
	<bean id="person" class="com.zking.entity.Person" >
	<!-- 注入有set注入 和构造注入 -->
	<!-- 自动掉用set注入-->
		<property name="pid" value="3"></property>
		<property name="pname" value="乐乐"></property>
		<!-- 集合注入 -->
		<property name="hobbies">
			<list>
				<value>王者农药</value>
				<value>宫爆老奶奶</value>
				<value>开心消消乐</value>
			</list>
		</property>
		<!-- 如何注入一个对象:ref引用 -->
		<property name="card" ref="card"></property>
		<!-- 构造注入 -->
		<!-- <constructor-arg value="1"></constructor-arg>
		<constructor-arg value="小小"></constructor-arg> -->
	</bean>
	<!-- lazy-init="true"懒加载 -->
	<bean id="card" class="com.zking.entity.Card">
		<property name="cid" value="10086"></property>
		<property name="cname" value="430424198923445888"></property>
	</bean>
</beans>
最后:测试

package com.zking.test;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

import com.zking.entity.Person;

public class TestSpring {
	@Test
	public void test(){
		//有延迟加载(懒加载)与及时下载的区别?
		//读取配置文件的bean
		//1.加载spring的配置文件
		//1.1有延迟加载(懒加载)延迟加载你要new一个它才会加载一个,但如果你是启动comcat很快
		//在spring中不管你new了多少个但在内存中它只占一个地址(可以提高性能)
		BeanFactory bf=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
		Person person1=(Person) bf.getBean("person");
		Person person2=(Person) bf.getBean("person");
		Person person3=(Person) bf.getBean("person");
		System.out.println(person1);
		System.out.println(person2);
		System.out.println(person3);
		//Spring设值可以去配置文件中设置
//		person1.setPid(2);
//		person1.setPname("侵略者");
//		System.out.println(person1);
		//1.2及时加载(加载配置文件)及时加载如果配置10个对象就会被全部加载出来,
                //但如果你是启动comcat的时候它会非常的慢,用的时候很快
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person=(Person) ac.getBean("person");
		System.out.println(person);
		System.out.println(person.getHobbies());
		System.out.println(person.getCard().getCname());
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值