zoukankan      html  css  js  c++  java
  • spring注入的四种方式

    配置文件spring.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:context="http://www.springframework.org/schema/context"

       xmlns:tx="http://www.springframework.org/schema/tx"

       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

               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

     

       <!-- 注入方式一,直接使用类全名注入,属性必须有相应的set方法才可以注入成功,否则会失败 -->

       <!-- 假如没有a属性,但是有setA方法,也可以配置a的property和value,但这样做并没有什么意义 -->

       <bean id="userDao1" class="com.colorlight.spring.UserDaoImpl">

          <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>

          <property name="driverClass" value="com.mysql.jdbc.Driver"></property>

          <property name="username" value="root"></property>

          <property name="password" value="1"></property>

       </bean>

     

       <!-- 注入方式二:使用静态的工厂方法创建实例,必须配置相应的工厂类和工厂方法 -->

       <bean id="userDao2" class="com.colorlight.spring.StaticDaoFactory"

          factory-method="createUserDaoInstance">

          <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>

          <property name="driverClass" value="com.mysql.jdbc.Driver"></property>

          <property name="username" value="root"></property>

          <property name="password" value="2"></property>

       </bean>

     

       <!-- 注入方式三:使用非静态的工厂方法创建实例,必须先生成工厂的实例 -->

       <bean id="daoFactory" class="com.colorlight.spring.NonStaticDaoFactory">

       </bean>

       <bean id="userDao3" factory-bean="daoFactory" factory-method="createUserDao">

          <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>

          <property name="driverClass" value="com.mysql.jdbc.Driver"></property>

          <property name="username" value="root"></property>

          <property name="password" value="3"></property>

       </bean>

     

       <!-- 注入方式四:使用构造函数注入 -->

       <bean id="userDao4" class="com.colorlight.spring.UserDaoImpl">

          <constructor-arg value="jdbc:mysql://localhost:3306/test" />

          <constructor-arg value="com.mysql.jdbc.Driver" />

          <constructor-arg value="root" />

          <constructor-arg value="4" />

       </bean>

    </beans>

     

    接口UserDao:

    package com.colorlight.spring;

     

    public interface UserDao {

       void printInfo();

    }

     

    实现类UserDaoImpl:

    package com.colorlight.spring;

     

    public class UserDaoImpl implements UserDao {

       private String jdbcUrl;

       private String driverClass;

       private String username;

       private String password;

     

       public UserDaoImpl(){

         

       }

       public UserDaoImpl(String url, String driver, String name, String pw) {

          this.jdbcUrl = url;

          this.driverClass = driver;

          this.username = name;

          this.password = pw;

       }

     

       public void printInfo() {

          System.out.println("jdbcUrl  = " + jdbcUrl);

          System.out.println("driverClass = " + driverClass);

          System.out.println("username = " + username);

          System.out.println("password = " + password);

       }

     

       public String getJdbcUrl() {

          return jdbcUrl;

       }

     

       public void setJdbcUrl(String jdbcUrl) {

          this.jdbcUrl = jdbcUrl;

       }

     

       public String getDriverClass() {

          return driverClass;

       }

     

       public void setDriverClass(String driverClass) {

          this.driverClass = driverClass;

       }

     

       public String getUsername() {

          return username;

       }

     

       public void setUsername(String username) {

          this.username = username;

       }

     

       public String getPassword() {

          return password;

       }

     

       public void setPassword(String password) {

          this.password = password;

       }

    }

     

    静态方法类:

    package com.colorlight.spring;

     

    public class StaticDaoFactory {

       public static UserDao createUserDaoInstance(){

          return new UserDaoImpl();

       }

    }

    非静态方法类:

    package com.colorlight.spring;

     

    public class NonStaticDaoFactory {

       public UserDao createUserDao() {

          return new UserDaoImpl();

       }

    }

     

    测试类:

    package com.colorlight.springscope;

     

    import org.junit.Test;

    import org.springframework.context.ApplicationContext;

    import org.springframework.context.support.ClassPathXmlApplicationContext;

     

    public class MainTest {

     

       ApplicationContext ac = new ClassPathXmlApplicationContext(

            "applicationContext.xml"this.getClass());

     

       // 单例

       @Test

       public void test1() {

          System.out.println("在getBean()调用之前");

          User user1 = (User) ac.getBean("user1");

          User user2 = (User) ac.getBean("user1");

          System.out.println(user1 != null);

          System.out.println(user1 == user2);

       }

     

       // 多例

       @Test

       public void test2() {

          System.out.println("在getBean()调用之前");

          User user1 = (User) ac.getBean("user2");

          User user2 = (User) ac.getBean("user2");

          System.out.println(user1 != null);

          System.out.println(user1 == user2);

       }

    }

     

  • 相关阅读:
    4月1日作业总结,成绩
    4月8日作业重点及点评
    构建之法 东北师大站 冲刺领跑
    4月1日作业重点及点评
    3月25日作业总结,成绩
    3月18日作业总结,成绩
    3月25日作业重点及点评
    python: 反射机制;
    python: 基本知识(一)
    opencv: 轮廓提取;
  • 原文地址:https://www.cnblogs.com/fabulousyoung/p/4073859.html
Copyright © 2011-2022 走看看