XML方式:java
IOC:控制反轉的底層原理就是:工廠模式+反射+配置文件
DI:依賴注入就是經過配置文件設置屬性值web
BeanFactory 是老版本的工廠類:調用getBean的時候,纔會生成類的實例
ApplicationContext 是新版本的工廠類:加載配置文件的時候,就會將Spring管理的類都實例化spring
ApplicationContext有兩個實現類:
ClassPathXmlApplicationContext :加載類路徑下的配置文件
FileSystemXmlApplicationContext :加載文件系統下的配置文件微信
<bean>標籤
id 使用了約束中的惟一約束,裏面不能出現特殊字符
name 沒有使用約束中的惟一約束(理論上能夠重複,實際開發中不能出現重複),能夠出現特殊字符mvc
bean生命週期的配置
init-method:Bean被初始化的時候執行的方法
destroy-method: Bean被銷燬的時候的方法(Bean是單例建立,工廠關閉)app
Bean做用範圍的配置:
scope Bean的做用範圍
singleton 默認的,Spring會採用單例模式建立這個對象
prototype 多例模式,用一次new一個(Struts2和spring整合的時候必定會用到)ide
P名稱空間的屬性注入
普通屬性 p:屬性名="值"
對象屬性 p:屬性名-ref="值"this
代碼以下spa
public interface UserService { public void save(); }
public class UserServiceImpl implements UserService { private String name; private String age; private String price; private Student student; public void setName(String name) { this.name = name; } public void setAge(String age) { this.age = age; } public void setPrice(String price) { this.price = price; } public void setStudent(Student student) { this.student = student; } @Override public void save() { System.out.println("save..name=" + name + ";age=" + age + ";price=" + price + ";student=" + student); } public void init() { System.out.println("init"); } public void destroy() { System.out.println("destroy"); } }
import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set; public class Student { private String name; private String[] arrs; private List<String> list; private Set<String> set; private Map<String, String> map; public void setName(String name) { this.name = name; } public void setArrs(String[] arrs) { this.arrs = arrs; } public void setList(List<String> list) { this.list = list; } public void setSet(Set<String> set) { this.set = set; } public void setMap(Map<String, String> map) { this.map = map; } @Override public String toString() { return "Student{" +
"name='" + name + '\'' +
", arrs=" + Arrays.toString(arrs) +
", list=" + list +
", set=" + set +
", map=" + map +
'}'; } }
配置文件prototype
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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="userDAO" class="com.jinke.demo.UserServiceImpl" init-method="init" destroy-method="destroy" p:age="30" p:price="2000" p:student-ref="student">
<property name="name" value="李東"/>
</bean>
<import resource="ApplicationContext2.xml"/>
</beans>
ApplicationContext2.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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="student" class="com.jinke.demo.Student" p:name="學生">
<property name="arrs">
<list>
<value>張三</value>
<value>李四</value>
<value>王五</value>
</list>
</property>
<property name="list">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
<property name="set">
<set>
<value>a</value>
<value>b</value>
<value>c</value>
</set>
</property>
<property name="map">
<map>
<entry key="1" value="a"/>
<entry key="2" value="b"/>
<entry key="3" value="c"/>
</map>
</property>
</bean>
</beans>
執行
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringDemo1 { @Test public void demo() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml"); UserService userService = (UserService) applicationContext.getBean("userDAO"); userService.save(); ((ClassPathXmlApplicationContext) applicationContext).close(); } }
獲得結果
save..name=李東;age=30;price=2000;student=Student{name='學生', arrs=[張三, 李四, 王五], list=[1, 2, 3], set=[a, b, c], map={1=a, 2=b, 3=c}} 六月 11, 2019 6:40:17 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@6842775d: startup date [Tue Jun 11 18:40:16 CST 2019]; root of context hierarchy destroy
註解的方式
Spring包括的模塊
web層:springmvc
service層:bean管理,聲明式事物
DAO層:ORM模塊、jdbc模板
IOC註解方式,能夠不提供set方法
屬性若是有set方法,註解添加到set方法上,沒有set方法就添加到屬性上
註解詳解
@Component:組件
衍生:@Controller web層的類、@Service service層的類、@Repository DAO層的類(通常用這個)
普通屬性:@Value
對象屬性:@Autowired 習慣是和@Qualifier一塊兒使用
@Resource(通常用這個)
生命週期相關注解
@PostConsruct 初始化
@PreDestroy 銷燬
Bean做用範圍
@Scope(singleton/prototype)
代碼以下
public interface UserDAO { public void save(); }
public interface UserService { public void save(); }
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Repository; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; @Repository(value = "userDao")//至關於<bean id = "userDao" class="com.jinke.demo1.UserDAOImpl"/>
public class UserDAOImpl implements UserDAO { @Value("大傻") private String name; /*@Value("大傻") public void setName(String name) { this.name = name; }*/
/*@Autowired @Qualifier(value = "userService222")*/ @Resource(name = "userService222") private UserService userService; @Override public void save() { System.out.println("UserDAOImpl被執行了--name=" + name); userService.save(); } @PostConstruct//至關於init-method
public void init() { System.out.println("UserDAOImpl被執行了init"); } @PreDestroy//至關於destroy-method
public void destroy() { System.out.println("UserDAOImpl被執行了destroy"); } }
import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; @Service("userService222") @Scope("singleton") public class UserServiceImpl implements UserService { @Override public void save() { System.out.println("UserServiceImpl的save被執行了"); } }
配置文件
<?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" 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.xsd"> <context:component-scan base-package="com.jinke.demo1"/> </beans>
執行
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Demo1 { @Test public void demo1() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext3.xml"); UserDAO userDao = (UserDAO) applicationContext.getBean("userDao"); userDao.save(); ((ClassPathXmlApplicationContext) applicationContext).close(); } }
輸出結果
UserDAOImpl被執行了init UserDAOImpl被執行了--name=大傻 UserServiceImpl的save被執行了 六月 12, 2019 4:25:36 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@6842775d: startup date [Wed Jun 12 16:25:36 CST 2019]; root of context hierarchy UserDAOImpl被執行了destroy
總結:通常XML用來管理Bean,註解完成屬性注入
歡迎關注個人微信公衆號:安卓圈