bean的作用范圍:
bean標簽中有scope屬性來指定:
singleton(siNGɡ?lt(?)n):單例模式 只創建一次 一致存在下去
prototype(?prōd??tīp):多例模式 每次使用都會重新創建
bean生命周期中的兩個方法(bean標簽屬性):
init-method: 執行構造后要進行的動作
destory-method:銷毀前要進行的動作
bean的生命周期是由容器來進行管理 容器關閉將銷毀bean ,不關閉前
需要手動進行銷毀,ApplicationContext的子類ClassPathXmlApplicationsContext
具有close方法。
銷毀的方法只能作用于單例模式 多例模式 由垃圾回收機制管理。
Spring的依賴注入:在ioc容器運行過程中動態將依賴關系注入到對象中
1.通過構造方法注入:通過<construct-arg ...... /> 在bean內容中書寫
標簽中的屬性: name:名稱 type:數據類型 index:索引 ref:賦值其他的bean類型 配置過的bean value:值
<bean name="date" class="java.util.Date"></bean> <bean id="student" class="com.offcn.entity.Student" > <constructor-arg name="id" value="1"></constructor-arg> <constructor-arg name="name" value="張三"></constructor-arg> <constructor-arg name="birthday" ref="date"></constructor-arg> </bean>
2.通過setter注入:<property name="" ref= ""> 書寫在bean內容中
<bean id="student" class="com.offcn.entity.Student"> <property name="id" value="2"></property> <property name="name" value="李四"></property> </bean>
3.通過p標簽:實質還是setter方法注入 換頭部 在bean標簽的中:p:name=""
<beans xmlns="http://www.springframework.org/schema/beans"201 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.offcn.entity.Student" p:id="1" p:name="李四"/>
配置文件模塊化:將一個配置文件 分為多個配置文件
創建applicationContext時可以傳入多個xml名,
在主配置文件中導入其他的配置文件
注:
在同一個xml文件中id不能重復 主配置文件與其他配置文件的bean的id
可以相同 ,但是后加載的bean會覆蓋前面的bean
spring注入數組 集合 properites類型數據
<bean id="commutils" class="com.offcn.entity.CommUtils"> <property name="array" > <array> <value>111111</value> <value>111111</value> </array> </property> <property name="list" > <list> <value>111111</value> <value>111111</value> </list> </property> <property name="array" > <array> <entry key ="1111" value="11111"></value> <entry key = "111" value ="1111"></value> </array> </property> <property name="properites" > <props> <prop name = "11">111111</value> <prop name="222" >111111</value> </props> </property>
Spring與jdbc的整合:
Spring提供了ioc容器,管理jdbc操作數據庫過程中需要的的數據庫連接對象,同時spring提供了整合jdbc操作數據
庫的工具類JdbcDaoSupport和模板工具JdbcTemplate,JdbcTemplate提供了大量操作數據庫的方法,
配置文件
注入的對象 必須是spring容器進行管理的對象
得到數據源

得到jdbcTemplate 注入 數據源的對象

得到userDao 注入 jdbcTemplate

得到UserService對象 注入userDao對象

JdbcTemplate的api
- query()查詢所以的數據

- queryForObject() 查詢單條記錄

Spring注解配置IOC
1.導入依賴
2.編寫配置文件:支持注解的頭部
<?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:annotation-config/> </beans>
3.編寫注解
@Component("userDao")
public class UserDaoImpl implements UserDao {
public int add() {
System.out.println("1111111111");
return 0;
}
}
4.在xml文件中掃描注解
<context:component-scan base-package="com.offcn"></context:component-scan>
5.測試
public class Test { @org.junit.Test public void show(){ ApplicationContext app = new ClassPathXmlApplicationContext("bean.xml"); UserDao userDao = (UserDao) app.getBean("userDao"); userDao.add(); } }
@Component:實例化當前對象 并使用spring來進行管理 value屬性與id作用相同 沒有的話就是當前類的名稱首字母小寫
@Repository:編寫dao層的注解
@Service:編寫service層的注解
@Controller:編寫表現層的注解
默認都是使用無參構造
@Autowired:在類中注入需要使用的對象 一般寫在方法或變量上 多在變量上
規則:根據類型進行注入 ,有且只能匹配一個,匹配多個或0個都會報錯
@qualified:按照名稱進行匹配 與上者結合使用 不能單獨使用 value進行設置匹配的名稱
java給spring提供的一個注解:
@Resource:jdk1.6之后 name 進行設置 作用上面兩個的結合 先類型匹配在名稱
設置單例
@Scope("singleton")
生命周期的方法注解:
@Predestory:對象銷毀前的動作
@PostConstruct:調用構造后的動作
<?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:annotation-config/>
</beans>
Spring中的注解:
實例化對象注解:
@Component:用于實例化對象 具有value屬性 寫在類的上方 當類不歸屬三層架構時使用
@Service:service層對象
@controller:
@Repository:持久層對象
依賴注入注解:
@AutoWired:按照類型進行注入 匹配0個或多個都會報錯 當時用該注解來注入屬性時 set方法可以省略
@Qualifier:在上者的基礎上在按照名稱進行匹配 具有 value屬性
@Value:用于簡單類型的注入
改變注入范圍的注解
@Scope
其他:
@Configuration:指定類為配置類取代xml文件 寫在類上
@Bean?。簩懺诜椒ㄉ?表示該方法創建一個對象 并放在Spring容器中,具有value屬性
@ComponentScan:執行spring初始化容器需要掃描的包 寫在類上
@PropertySource:加載properites文件 寫在類上
與junit整合
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:.xml文件或配置類.class")
浙公網安備 33010602011771號