spring基礎學習01
阿新 • • 發佈:2019-09-29
spring基礎
Spring是一個開放原始碼的設計層面框架,他解決的是業務邏輯層和其他各層的鬆耦合問題,因此它將面向介面的程式設計思想貫穿整個系統應用
IOC控制反轉
把建立物件和維護物件之間的關係權利轉交給spring管理,spring容器控制物件的建立,注入需要注入的物件
aop面向切面程式設計
通過預編譯方式和執行期動態代理實現程式功能的統一維護的一種技術
隔離業務邏輯,降低耦合度,提高可用性和開發效率,主要用於日誌記錄,事務管理,異常處理等等
模組化
3.0版本後,根據需要引入模組需要的包,進行模組開發
小demo
測試配置檔案
建立物件
@Data public class Person { private String name; }
建立配置檔案,為了方便測試放在同級目錄下
<?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.xsd"> <bean id="person" class="top.mjsmry.demo01.Person" name="person"> </bean> </beans>
建立測試方法
public class SpringDemoTest { @Test public void tes01() { //例項化管理物件 ClassPathResource resource = new ClassPathResource("/top/mjsmry/demo01/spring-config.xml"); XmlBeanFactory factory = new XmlBeanFactory(resource); //1根據id建立 Person u = (Person) factory.getBean("person"); u.setName("lili"); System.out.println(u); } }
三種獲取例項化的方式,上面已經寫了一種
//2根據name和class
Person person = factory.getBean("person2", Person.class);
person.setName("lili");
//3直接根據位元組碼 但是配置檔案必須唯一,一個物件配置多個bean的話,spring會無法選擇
Person bean = factory.getBean(Person.class);
System.out.println(person);
Application和BeanFactory
- 建立物件的時機是
- ApplicationContext在建立工廠的時候就會把bean例項化,優先載入
- BeanFactory使用物件時才會例項化
- 關係
- ApplicationContext實現BeanFactory
- ApplicationContext更強大
- ApplicationContext實現懶載入
- 全域性:default-lazy-init="true"
單獨節點: lazy-init="true"
spring測試
spring測試可以在不啟動spring專案情況下進行單元測試
@ContextConfiguration("spring-config.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringTest {
@Autowired
Person person;
@Test
public void test() {
person.setName("lili");
System.out.println(person);
}
}
singletonAndprototype
<bean id="person" class="top.mjsmry.singletonAndprototype.Person" scope="prototype"/>
scope prototype多例 singleton單例
public class SpringTest {
@Test
public void test() {
ApplicationContext ac=new ClassPathXmlApplicationContext("top/mjsmry/singletonAndprototype/spring-config.xml");
Person person1= (Person) ac.getBean("person");
Person person2= (Person) ac.getBean("person");
//單例的話就是同一個物件
System.out.println(person1==person2);//true
}
}
bean與bean之間的關係
繼承
- 通過parent指定繼承
- 有abstract屬性的bean不能被例項化
子bean可以覆蓋父bean
依賴
- 可以通過depends-on="其他beani定依賴關係
- 如果依賴了abstract的bean也不能例項化
<!-- 模板bean -->
<bean id="p" class="top.mjsmry.beanAndBean.Person" abstract="true">
<property name="name" value="張三"></property>
</bean>
<bean id="person" class="top.mjsmry.beanAndBean.Person" parent="p"/>
<bean id="person2" class="top.mjsmry.beanAndBean.Person" parent="p">
<property name="name" value="子覆蓋了"></property>
</bean>
@Test
public void test() {
ApplicationContext ac=new ClassPathXmlApplicationContext("top/mjsmry/beanAndBean/spring-config.xml");
Person person= (Person) ac.getBean("person");
System.out.println(person);
Person person2= (Person) ac.getBean("person2");
System.out.println(person2);
}
生命週期
基本宣告週期
<!-- 生命週期
構造方法
getset
init-method 初始化
destroy-method 銷燬
-->
<bean id="person" class="top.mjsmry.beancycle.Person" init-method="init" destroy-method="destory"/>
bean
@Data
public class Person {
private String name;
public Person() {
System.out.println("構造方法呼叫了");
}
private void init() {
System.out.println("--init--");
}
private void destory() {
System.out.println("--destory--");
}
}
補充週期
<!--
實現BeanPostProcessor 細緻的宣告週期
postProcessBeforeInitialization 初始化方法之前
postProcessAfterInitialization 初始化方法之後
-->
<bean id="personBeanPostProcessor" class="top.mjsmry.beancycle.PersonBeanPostProcessor"/>
實現介面
public class PersonBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
System.out.println("postProcessBeforeInitialization");
return o;
}
@Override
public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
System.out.println("postProcessAfterInitialization");
return o;
}
}
測試
@Test
public void test() {
ClassPathXmlApplicationContext ca=new ClassPathXmlApplicationContext("top/mjsmry/beancycle/spring-config.xml");
Person person= (Person) ca.getBean("person");
ca.close();
}
注入測試
spring原生注入方式實現三層架構
dao
public class TestDao {
public int add() {
return 1;
}
}
service
public interface TestService {
String add();
}
@Data
public class TestServiceImpl implements TestService {
private TestDao testDao;
@Override
public String add() {
return testDao.add()==1?"新增成功":"新增失敗";
}
}
controller
@Data
public class TestController {
private TestService testService;
public String add() {
return testService.add();
}
}
配置檔案
<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.xsd">
<!-- set注入 -->
<bean id="testController" class="top.mjsmry._01.controller.TestController">
<property name="testService" ref="testService"/>
</bean>
<bean id="testService" class="top.mjsmry._01.service.impl.TestServiceImpl">
<property name="testDao" ref="testDao"/>
</bean>
<bean id="testDao" class="top.mjsmry._01.dao.TestDao"></bean>
</beans>
test
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring-config.xml")
public class MyTest {
@Autowired
private TestController testController;
@Test
public void test01() {
String result = testController.add();
System.out.println(result);
}
}
測試結果
注入BasicDataSource
<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"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
">
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置一個DBCP的Bean -->
<bean name="dateSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<!-- 注意:這裡我們不是使用的ref引用,而是直接寫的value,因此注入的資料是一個變通的值 -->
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/jdbcwork?useSSL=false&serverTimezone=UTC
jdbc.username=xxx
jdbc.password=xxx
測試
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("spring-config.xml")
public class DataSourceTest {
@Autowired
BasicDataSource basicDataSource;
@Test
public void test01() {
try {
Connection connection = basicDataSource.getConnection();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
無報錯測試通過
其他注入
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private String username;
private Car car;
private String[] strings;
private List<String> list;
private Set<String> set;
private List<Wife> wifeList;
private Properties p1;
private Properties p2;
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 外部bean注入 -->
<bean id="car" class="top.mjsmry._05other.Car">
<property name="price" value="1.0"/>
<property name="type" value="bwm"/>
</bean>
<bean id="person" class="top.mjsmry._05other.Person">
<property name="username" value="張三"/>
<property name="car" ref="car"/>
</bean>
<!-- 內部bean定義 -->
<bean id="person2" class="top.mjsmry._05other.Person">
<property name="username" value="張三"/>
<property name="car">
<bean class="top.mjsmry._05other.Car">
<property name="price" value="1.0"/>
<property name="type" value="bwm"/>
</bean>
</property>
</bean>
<!-- 其他型別的注入 -->
<bean id="person3" class="top.mjsmry._05other.Person">
<property name="username" value="張三"/>
<property name="car">
<bean class="top.mjsmry._05other.Car">
<property name="price" value="1.0"/>
<property name="type" value="bwm"/>
</bean>
</property>
<!-- 陣列 -->
<property name="strings" value="lili,keke"/>
<!-- 集合 -->
<property name="list">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
<!-- set -->
<property name="set">
<set>
<value>k</value>
<value>e</value>
<value>w</value>
</set>
</property>
<!-- 泛型 -->
<property name="wifeList">
<list>
<bean class="top.mjsmry._05other.Wife">
<property name="username" value="lili"/>
</bean>
</list>
</property>
<!-- Properties注入-->
<property name="p1">
<value>proKey1=proValue1</value>
</property>
<property name="p2">
<props>
<prop key="鍵1">值1</prop>
</props>
</property>
</bean>
</beans>
測試
public class OtherTest {
@Test
public void test01() {
ApplicationContext ac=new ClassPathXmlApplicationContext("/top/mjsmry/_05other/spring-config.xml");
Person person= (Person) ac.getBean("person");
Person person2= (Person) ac.getBean("person2");
Person person3= (Person) ac.getBean("person3");
System.out.println(person);
System.out.println(person2);
System.out.println(person3);
}
}
關注我的個人部落格林中小屋
更多內容關注我的個人部落格林中小屋