发表于: 2017-04-27 22:19:59
1 1323
今天完成的事情:
一.了解了spring注入的第二种方式:内部bean
例子:
<!-- 采用内部bean的方法 -->
<bean id="personService" class="service.PersonServiceBean">
<property name="personDao">
<!-- 注意:这里没有了ref-->
<bean class="dao.PersonDaoBean"></bean>
</property>
</bean>
二. 学习了注入集合类型的对象:(是由property控制)
1、在要注入到的类中定义并生成相应的getter、setter方法;
2、在bean.xml文件中添加相应的配置;
3、要将生成的get方法放入到接口中,这样客户端才能访问;
Set类型:
<bean id="personDao" class="dao.PersonDaoBean"></bean>
<bean id="personService" class="service.PersonServiceBean">
<property name="sets">(属性名称)
<set>
<value>admin1</value> (指定集合中某个元素的值)
<value>admin2</value>
<value>admin3</value>
</set>
</property>
</bean>
List类型:
<property name="lists">
<list>
<value>admin4</value>
<value>admin5</value>
<value>admin6</value>
</list>
</property>
map类型:
<property name="maps">
<map>
<entry key="key-1" value="value-1"></entry>
<entry key="key-2" value="value-2"></entry>
<entry key="key-3" value="value-3"></entry>
</map>
</property>
业务层核心代码:(以map为例)
public class PersonServiceBean implements PersonService {
//定义类对象的属性personDao
private PersonDao personDao;
//定义基本数据类型username id
private String username;
private int id;
//定义属性集合maps
private Map<String,String> maps = new HashMap<String,String>();
//一定要添加的get方法
public Map<String, String> getMaps() {
return maps;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
//重写接口中的save方法
public void save(){
personDao.add();
System.out.println("name:"+username);
System.out.println("id:"+id);
}
}
客户端调用:
举例:对于map集合
package test;
import impls.PersonService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
public static void setUpBeforeClass(){
}
public static void instanceSpring(){
//根据配置文件,创建一个spring容器的实例
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
//从容器中,根据id,取得对象
PersonService ps =(PersonService)ctx.getBean("personService");
for(String key :ps.getMaps().keySet()){
System.out.println("value:"+ps.getMaps().get(key));
}
}
}
接口申明:
注意:在PersonServiceBean 中实现的是PersonService 接口,所以 ,接口中的定义有取得集合元素的方法:
package impls;
import java.util.List;
import java.util.Map;
import java.util.Set;
public interface PersonService {
public abstract void save();
public Set<String> getSets();
public List<String> getLists();
public Map<String, String> getMaps();
}
对于集合类型都为XXXGet方法,取得集合中的元素值。
二、了解了构造器注入的方法(具体的就不写出来啦)
三、学习了利用注解字段的方式,进行bean的注入(这个只学了一半,明天如果时间够会看完的)
配置文件:(如果要使用注解方式,必须加入以下命名空间)
<?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-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<!--这个隐式配置注册了多个对注释进行解析处理的处理器-->
<!--这些bean都是用来定义实体类的-->
<bean id="personDao" class="dao.PersonDaoBean"></bean>
<bean id="personService" class="service.PersonServiceBean"></bean>
</beans>
评论