Spring框架簡介③
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
相關文章
- Spring框架簡介⑩Spring框架
- Spring框架簡介⑨Spring框架
- Spring框架簡介⑧Spring框架
- Spring框架簡介⑦Spring框架
- Spring框架簡介⑥Spring框架
- Spring框架簡介⑤Spring框架
- Spring框架簡介④Spring框架
- Spring框架簡介②Spring框架
- Spring框架簡介①Spring框架
- Spring 簡介Spring
- Scrapy框架簡介框架
- SpringMVC框架簡介②SpringMVC框架
- SpringMVC框架簡介①SpringMVC框架
- Hibernate框架簡介⑤框架
- Hibernate框架簡介④框架
- Hibernate框架簡介③框架
- Hibernate框架簡介②框架
- Hibernate框架簡介①框架
- HTML 框架簡介HTML框架
- spring框架的介紹Spring框架
- Spring Batch 簡介SpringBAT
- Spring框架之IOC介紹Spring框架
- [轉]SSH框架簡介框架
- Spring常用Annotation簡介Spring
- Spring_Framework_簡介SpringFramework
- Spring系列.Bean簡介SpringBean
- Spring(2)-AOP簡介Spring
- Flutter路由框架Fluro簡介Flutter路由框架
- uni-app 框架簡介APP框架
- wsgiref模組、web框架、django框架簡介Web框架Django
- Spring Security中AuthorizationManager簡介Spring
- Spring AOP基礎簡介Spring
- Spring Cloud Gateway使用簡介SpringCloudGateway
- Spring中雲事件簡介Spring事件
- Spring 框架基礎(06):Mvc架構模式簡介,執行流程詳解Spring框架MVC架構模式
- Spring框架系列(2) - Spring簡單例子引入Spring要點Spring框架單例
- 工作流框架Flowable 簡介框架
- OSX 攻擊框架Empyre簡介框架