Spring框架簡介③

Milky-way發表於2018-08-07

Spring環境的搭建:

1 建立普通java工程即可

    按照之前的建立的工程來就行:

    

2 新增jar包+log4j

    連結:https://pan.baidu.com/s/1yAGgCHCY2yL1F93Qv0hHTw 密碼:l9q5

3 建立配置檔案(預設檔名為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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

</beans>

4建立bean

以上面的工程舉例:

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

    <!-- 建立bean -->
    <bean id="userDao" class="com.rl.spring.dao.impl.UserDaoImpl"></bean>
</beans>

測試程式碼:

package com.rl.spring.service;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.rl.spring.dao.UserDao;

class UserServiceTest {

    @Test
    void testSave() {
        
        //初始化Spring容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:ApplicationContext.xml");
        //獲取bean
        UserDao userDao = (UserDao) ctx.getBean("userDao");
        //呼叫
        userDao.save();
    }
}

輸出:

user被儲存了!

 

bean的建立方式:

1 使用構造器的方式建立(90%使用這種方式建立bean)

    上面的方式建立bean就是使用類的預設構造器的方式建立的, 如果沒有預設構造器的話是無法直接這樣建立的

2 靜態工廠方式建立

    User類:

package com.rl.spring.model;

public class User {
	
	private Integer userId;
	
	private String username;
	
	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
}

    靜態工廠類:

package com.rl.spring.factory;

import com.rl.spring.model.User;

public class FactoryBean {
	
	/**
	 * 必須是靜態方法
	 * @return
	 */
	public static User createUser(){
		return new User();
	}
}

    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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

	<bean id="user" class="com.rl.spring.factory.FactoryBean" 
	factory-method="createUser"></bean>
	
</beans>

    測試程式碼:

package com.rl.spring.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.rl.spring.dao.UserDao;
import com.rl.spring.model.User;

public class TestSpring {

	@Test
	public void test() {
		ApplicationContext ctx =
				new ClassPathXmlApplicationContext("ApplicationContext.xml");
		User user = (User) ctx.getBean("user");
		System.out.println(user);
	}
}

輸出:

com.rl.spring.model.User@b1ffe6

3例項工廠方式建立

User類:

package com.rl.spring.factory;

import com.rl.spring.model.User;

public class FactoryBean1 {
	
	public User createUser(){
		return new User();
	}
}

配置檔案:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

	
	<!-- 通過Spring來定義例項工廠 -->
	<bean id="factoryBean" class="com.rl.spring.factory.FactoryBean1"></bean>
	<!-- 指定要建立的bean
		factory-bean:指定例項工廠類,
		factory-method:工廠的建立bean的方法
	 -->
	<bean id="user" factory-bean="factoryBean" factory-method="createUser"></bean>
</beans>

工廠類:

package com.rl.spring.factory;

import com.rl.spring.model.User;

public class FactoryBean1 {
	
    /**
     * 不需要static
     * @return
     */
	public User createUser(){
		return new User();
	}
}

測試程式碼:

package com.rl.spring.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.rl.spring.model.User;

public class TestSpring {

	@Test
	public void test() {
		ApplicationContext ctx =
				new ClassPathXmlApplicationContext(new String[]{"ApplicationContext.xml"});
		User user = (User) ctx.getBean("user");
		System.out.println(user);
	}
}

輸出:

com.rl.spring.model.User@173f6d9

 

 

相關文章