spring学习一

1.新疆一个web工程添加如下jar

2.新建配置文件applicationContext.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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
            http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    <util:properties id="jdbc" location="classpath:jdbc.properties">
    </util:properties>
    <bean id="userbean" class="bean.UserBean"></bean>
    
    <bean id="testZhuru" class="bean.TestZhuRU">
        <property name="name" value="#{jdbc.name}"></property>
    </bean>
    
    <bean id="dataSource" class="util.JDBCDataSource">
        <property name="driver" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/study"></property>
        <property name="user" value="root"></property>
        <property name="pwd" value="123456"></property>
    </bean>

     <!-- 构造器注入 -->
    <bean id="userDAO" class="daoimp.UserImpl">
        <!-- 利用构造器参数注入bean的属性 -->
        <constructor-arg name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="UserSerivice" class="service.serviceimpl.UserSeriviceImpl" autowire="byType"/>
   
    <!-- <bean id="user2" class="bean.Userbean" factory-method="getInstance"></bean> -->
    <!-- 1. 用构造器来实例化的方式的配置代码如下: -->
    <bean id="calendarObj1" class="java.util.GregorianCalendar"></bean>
    <!-- 2. 使用静态工厂方法实例化的方式的配置代码如下: -->
    <bean id="calendarObj2" class="java.util.Calendar" factory-method="getInstance">
    </bean>
    <!-- 3. 使用实例工厂方法实例化的方式的配置代码如下: -->
    <bean id="calendarObj3" class="java.util.GregorianCalendar"></bean>
    <bean id="dateObj" factory-bean="calendarObj3" factory-method="getTime">
    </bean>
</beans>

3.实体bean

package bean;

public class UserBean {
    private String username;
    private String password;
    private String name;

    private static UserBean instance = null;

    public static UserBean getInstance() {
        if (instance == null) {
            synchronized (UserBean.class) {
                if (instance == null) {
                    instance = new UserBean();
                }
            }
        }
        return instance;
    }

    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;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return name+","+username+","+password;
    }
    
}

4.测试方法

    public void TestInitContext(){
        String conf = "applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//        Userbean user = (Userbean)ac.getBean("Userbean");
//        user.setName("测试");
//        System.out.println(user.getName());
        UserBean user2 = (UserBean)ac.getBean("user2",UserBean.class);
        user2.setName("静态工厂");
        System.out.println(user2.getName());
        
         //2. 使用静态工厂方法实例化的方式。
        //利用Spring调用 Calendar 的静态工厂方法getInstance() 创建 Calendar实例.
        Calendar cal2 = ac.getBean("calendarObj2", Calendar.class);
        System.out.println("cal2:"+cal2);
    }



你可能感兴趣的:(spring学习一)