1.1. 依赖注入
当我们把依赖对象交给外部容器去创建时,那么PersonServiceBean类可修改如下:
public class PersonServiceBean{
private PersonDao persondao;
//通过构造参数,让容器把创建好的依赖对象注入到PersonServiceBean中,当然也可以通过setter方法进行注入。
public PersonServiceBean(PersonDao persondao){
this.persondao = persondao;
}
public void save(){
persondao.add();
}
}
<bean id="persondao" class="com.sun.demo.PersonDaoBean" />
<bean id="personService" class="com.sun.demo.PersonServiceBean" >
<property name="persondao" ref="persondao" />
</bean>
//通过setter方法注入而不能同时存在构造方法。
<constructor-arg ref="persondao" />
//构造器注入
注意:在配置文件中若同时配置了构造器和setter方法注入,那么类中也必须对应的有构造方法和setter方法,后台配置中用setter方法则类中不能有构造器对注入属性进行赋值。两种方法必须对应出现,优先以构造方法为主。
<property name="persondao">
<bean class="com.sun.demo.PersonDaoBean" />
</property>
//区别于以上两种方式,这个是内部注入的方式,这个bean只能为当前bean调用
除此以外,还可以对一些基本类型属性进行赋值
<property name="username" value="zhanshan" />
集合类型的装配:
对于一些集合类型的属性
private Set<String> sets = new HashSet<String>();
<bean id="personService" class="com.sun.demo.PersonServiceBean">
<property name="sets">
<set><value>需要注入的值</value></set>
</property>
<property name="properties">
<props><prop key="key值">value值</prop></props>
</property>
<property name="maps">
<map><entry key="key值" value="value值" /></map>
</property>
</bean>
前台打印方式:
for(String value : personService.getSets()){
System.out.println(value);
}
for(Object key : personService.getProperties().keySet()){
System.out.println(personService.getProperties().getProperty((String)key));
}
for(String key : personSetvice.getMaps().keySet()){
System.out.println(personSerive.getMaps().get(key));
}
使用Field注入【用于注解方式】
1.2. 实例化spring容器
(1)Resource resource = new ClassPathResource("applicationContext.xml");
XmlBeanFactory factory = new XmlBeanFactory(resource);
(2)InputStream is = new FileInputStream("d:/config/applicationContext.xml");
Resource rs = new InputStreamResource(is);
XmlBeanFactory factory = new XmlBeanFactory(rs);
(3)ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
(4)ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[] {"d:\\bean.xml"});
(5)String[] configFile = {applicationContext.xml};
ApplicationContext ctx = new ClassPathXmlApplicationContext(configFile);
BeanFactory bean = (BeanFactory)ctx;
1.3. 连接Hibernate访问数据库
(1)Resource resource = new ClassPathResource("applicationContext.xml");
XmlBeanFactory factory = new XmlBeanFactory(resource);
SessionFactory sessionFactory = factory.getBean("mySessionFactory");
//mySessionFactory在配置文件中已经配置好
HibernateTemplate template = new HibernateTemplate();
template.setSessionFactory(sessionFactory);
List list = template.find("select userId from com.sun.demo.UserInfo");
(2)主要就是获取配置文件中的上下文,进而提取Bean节点,另一种方法就是将其设为类的属性,在生成一个类的时候自动加载配置文件中的信息进行依赖注入属性值
template.find();
template.load();
template.save();
template.update();
template.get();
(3)部分代码同上
HibernateTemplate ht = new HibernateTemplate(mySessionFactory);
final String hql = "update UserInfo set password = ''1234";
Object obj = ht.execute(new HibernateCallBack()){
public Object doInHibernate(Session session)throws HibernateException,....{
String hql = "update ......";
Query query = session.createQuerty(hql);
return query.execueteUpdate();
}
};
HibernateDaoSupport
可以简化模板,不用再生成新对象HibernateTemplate,也不用可以类属性SessionFactory,直接用this.getHibernateTemplate()
JdbcDaoSupport
注意:同种方法有不同的实现体,即重载方法,可以进行参数的动态给予。
1.4. SSH框架整合
注意添加需要的jar包
(1)新建一个action类继承ActionSupport,则:
ApplicationContext ctx = this.getWebApplicationContext();
在struts-config.xml文件中配置如下信息加载applicationContext.xml
<plug-in calssName="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property
property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>
(2)在web.xml文件中配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
(3)在web.xml中配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>自定义名称</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
以上三种方式都是在action类继承了ActionSupport之后的配置
(4)IOC依赖注入,继承SSH框架
以往我们在访问login.do的时候都是根据配置文件,去查找对应得Action类
<action path="/login" scope="request" type="com.sun.demo.LoginAction" />
现在我们需要修改一下type的值
<action path="/login" type="org.springframework.web.struts.DelegatingActionProty" />
同时需要以上三种配置方式中的一种方式来指定查找的spring配置文件applicationContext.xml,在spring的配置文件中根据path去查找bean节点的name值,进而去访问action类,同时对于某些可能用到的用户自定义类也可以配置为Action类的属性,进而直接调用。