spring IOC简单应用(一)

来源:互联网 发布:java视频播放缓冲技术 编辑:程序博客网 时间:2024/05/01 14:27

一、IOC的优势

    1、本来是由应用程序管理的对象之间的依赖关系,现在交给容器进行管理;

     2、大量的减少了工厂类与单列类的数量,使得代码层次更加清晰;

     3、是一个轻量级容器,没有侵入性;

二、注入方式(常用)

    1、构造函数注入

public class UserServiceImpl implements UserService{private UserDao dao;public UserServiceImpl(){}public UserServiceImpl(UserDao dao){this.dao = dao;}public void addUser(String name, String pass){this.dao.addUser(name, pass);}}
  2、spring的配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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-3.0.xsd">    <bean id="userDao4MySql" class="com.liusheng.dao.impl.MySqlDaoImpl"></bean>    <bean id="userDao4Oracle" class="com.liusheng.dao.impl.OracleDaoImpl"></bean>    <bean id="userService" class="com.liusheng.service.impl.UserServiceImpl">       <!-- 构造函数注入 -->       <constructor-arg index="0" ref="userDao4MySql">       </constructor-arg>    </bean></beans>
  3、java应用客户端调用代码

public class Client{public static void main(String[] args){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");        UserService service = (UserService)context.getBean("userService");        service.addUser("zhangsan", "lisi");}}

  2、set方法注入

      
public class UserServiceImpl implements UserService{private UserDao dao;public void addUser(String name, String pass){this.dao.addUser(name, pass);}public UserDao getDao(){return dao;}public void setDao(UserDao dao){this.dao = dao;}}

   2、spring的配置文件applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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-3.0.xsd">    <bean id="userDao4MySql" class="com.liusheng.dao.impl.MySqlDaoImpl"></bean>    <bean id="userDao4Oracle" class="com.liusheng.dao.impl.OracleDaoImpl"></bean>     <bean id="userService2" class="com.liusheng.service.impl.UserServiceImpl">       <!-- setter方法注入 -->       <property name="dao" ref="userDao4Oracle"></property>    </bean> </beans>

  3、java应用客户端调用代码

public class Client{public static void main(String[] args){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");        UserService service = (UserService)context.getBean("userService2");        service.addUser("zhangsan", "lisi");}}

 3、常用属性注入

     1、spring自带的属性类型转换(包括对 int  string  list  set   map  )

public class Bean{private String strValue;private int intValue;private List listValue;private Set setValue;private String[] arrayValue;private Map mapValue;//...setter/getter...}
               spring 配置文件中的注入

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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-3.0.xsd">        <!-- 常用屬性的配置 -->    <bean id="bean" class="com.liusheng.clientr.Bean">        <!-- setter方法注入 -->        <property name="strValue" value="hello_world"></property>        <property name="intValue" value="123"></property>        <property name="listValue">           <list>              <value>123</value>              <ref local="userDao4MySql"/>           </list>        </property>        <property name="setValue">            <set>                <ref local="userDao4Oracle"/>                <value>121212</value>            </set>        </property>        <property name="arrayValue">           <list>              <value>111</value>           <value>222</value>           <value>333</value>           </list>        </property>        <property name="mapValue">           <map>              <entry key="1" value="111"></entry>              <entry key="2" value="222"></entry>              <entry key="3" value="333"></entry>           </map>        </property>    </bean></beans>

  2、spring并不能对Date类型进行属性转换,需要自定义属性类型转换器(继承PropertyEditorSupport)  了,  自定义属性编辑器:Date(自定义的需要继承PropertyEditorSupport)重写setAsText()方法
/** * spring 没有提供对日期类型的自动转换器,需要我们 * 自己编写一个类型转换器。 */public class UtilDatePropertyEditor extends PropertyEditorSupport{public void setAsText(String text) throws IllegalArgumentException{try{Date date = new SimpleDateFormat("yyyy-mm-dd").parse(text);this.setValue(date);} catch (ParseException e){e.printStackTrace();}}}
       然后在属性文件中进行配置该转换器(applicationContext.xml)

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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-3.0.xsd">        <!-- 客制化类型转换 -->    <bean id="customEditors" class="org.springframework.beans.factory.config.CustomEditorConfigurer">       <property name="customEditors">           <map>              <entry key="java.util.Date">                 <bean class="com.liusheng.conversion.UtilDatePropertyEditor"></bean>              </entry>           </map>       </property>    </bean></beans>
     当在配置文件中配置好了之后,spring会自动的去寻找对应的类型转换对日期进行转换通过我们自定义的转换器。
    

     




原创粉丝点击