spring簡單總結part2

 

6. SpringBean管理(基於註解方式)

6.1 簡要步驟

1. 導入jar java

HelloWorld中的必備包便可(與註解相關的jar包是context) mysql

2. 引入Spring的配置文件 web

       注意:用註解開發時,必定要導入context的約束文件 spring

       導入約束文件,在配置文件中添加約束的步驟,請參看4.2-4.3 sql

可是必定要注意: express

在配置文件中context的添加約束文件時,已經有的就不要再添加了,只須要添加新引入的 session

context約束,並且, prefix不能夠爲空,自行填寫,可是建議填context app

添加完成後: dom

       <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ide

              xmlns="http://www.springframework.org/schema/beans"

              xmlns:context="http://www.springframework.org/schema/context"

              xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.2.xsd ">

       </beans>

3. 編寫實體類,並在實體類中使用註解開發

@ Component (value="annotation") 

//至關於配置文件中的<bean name=」 annotation」class=」xxx.xx. MyAnnotation」></bean>

public class MyAnnotation {

       @Value(value = "xiaoxiao")

//至關於配置文件中的<property name=」name」 value=」xiaoxiao」></property>

       private String name;

       public String getName() {

              return name;

       }

       public void setName(String name) {

              this.name = name;

       }

       @Override

       public String toString() {

              return "MyAnnotation [name=" + name + "]";

       }

      

}

4. 在配置文件中,添加要掃描註解的包(重點,必不可少!!!)

<context:component-scan base-package="cn.xiaoge.domain"></context:component-scan>

5. 編寫測試類

@Test

       public void fun1(){

              //因爲配置文件放在了包下面,在加載配置文件時,要填寫完整路徑

ClassPathXmlApplicationContext ac =

new ClassPathXmlApplicationContext("cn/xiaoge/annotation/annotation.xml");

              MyAnnotation myann = (MyAnnotation) ac.getBean("annotation");

             

              System.out.println(myann);

             

       }

6.2 經常使用註解

@Component:組件: 至關於在配置文件中註冊bean

       Spring提供了@Component的註解的一些衍生註解:(更易理解)

              * @Controller :     標識web層的javabean

              * @Service:        標識爲service層的javabean

              * @Repository      :     標識爲dao層的javabean

@Value:注入普通類型的屬性

@Autowired  (主要針對引用類型的數據封裝)

默認按類型完成屬性的注入, 若是存在兩個同樣的引用類型,則會報錯:

              * 所以,習慣採用按名稱注入,

              * 強制使用按名稱的方式完成屬性的注入:

                     * @Qulifer(value=」名稱」)

@Resource 至關於 @Autowired + @Qulifer

@PostConstruct:   添加該註解的方法爲初始化方法, 至關於init-method

@PreDestory:   添加該註解的方法爲銷燬方法, 至關於destroy-method

@Scope: 註解用來修飾bean的做用域

至關於scope屬性:

              * singleton    單例,只容許生成一個對象,用戶訪問的也是同一個

              * prototype   多例,每次訪問都會生成一個新的對象

              * request     一次請求

              * session     一次會話

6.3 Spring3.0基於JavaConfig爲核心的註解(基本不用)

JavaConfig爲核心:使用Java類做爲配置文件.

              * 類的構造特別麻煩!!!

@Configuration

public class BeanConfig {

       @Bean(name="car")

       public Car showCar(){

              Car car = new Car();

              car.setName("馬自達");

              car.setPrice(150000d);

              return car;

       }

      

       @Bean(name="product")

       public Product showProduct(){

              Product product = new Product();

              product.setName("空調");

              product.setPrice(1200d);

              return product;

       }

}

6.4 Springxml配置與註解配置的比較

XML:結構清晰.(Bean管理由Spring控制.)

註解:開發便捷.(屬性注入:不須要提供set方法.)

企業中一般還有一種開發方式:XML和註解的整合開發.

              * XML用於管理Bean.

              * 註解用於屬性注入.

須要在配置文件中開啓註解配置:

<context:annotation-config/>

Bean交給Spring進行管理.屬性注入由註解完成.

7. Springweb上的部署

7.1 導入jar

核心jar

       context          beans            core               expression            web

                     logging                               lo4j

其中:web包是爲了保證配置文件只加載一次

7.2 web.xml中配置

<!-- 配置監聽器 -->

  <listener>

      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

<!-- 配置文件的存放位置 -->

  <context-param>

      <param-name>contextConfigLocation</param-name>

      <param-value>classpath:applicationContext.xml</param-value>

  </context-param>

7.3 web層測試

WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(xxx);

//servlet, xxxgetServletContext()

//action,xxx能夠是ServletActionContext().getServletContext()

              Car car = (Car) ac.getBean("car");

              System.out.println(car);

8. Spring整合junit

8.1 導入jar

web所需包   +  test +  依賴包中的junit

8.2 在測試類中

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations="classpath:applicationContext.xml")

public class TestJunit {

      

       @Resource(name="user2")

       private User user;

      

       @Test

       public void fun1(){

              System.out.println(user);

       }

}

9. Spring整合jdbcTemplete()

9.1 導入jar

com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar

mysql-connector-java-5.0.8-bin.jar

9.2 xml中配置

<!-- 配置properties文件-->

<context:property-placeholder location="classpath:cn/xiaoge/l_jdbcTemplate/db.properties"/>

       <bean name="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

              <property name="driverClass" value="${driverClass}" ></property>

              <property name="jdbcUrl" value="${url}" ></property>

              <property name="user" value="${user}" ></property>

              <property name="password" value="${password}" ></property>

              <property name="maxPoolSize" value="5" ></property>

       </bean>

      

       <!-- 配置jdbc模板,並且必定要注意:dataSource要大寫,大寫!!!! -->

       <bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >

              <property name="dataSource" ref="datasource" ></property>

       </bean>

      

       <!-- 配置UserDao -->

       <bean name="userDao" class="cn.xiaoge.l_jdbcTemplate.UserDaoImpl" >

              <property name="jt" ref="jdbcTemplate" ></property>

       </bean>

9.3 UserDaoImpl中添加屬性jt

private JdbcTemplate jt;

       // setter方法必定要加

       public JdbcTemplate getJt() {

              return jt;

       }

       public void setJt(JdbcTemplate jt) {

              this.jt = jt;

       }

9.4 在測試類中進行測試

@Test

       public void fun1(){

              User u = new User();

              u.setName("jdbcDaoTemplete");

              ud.save(u);

}
相關文章
相關標籤/搜索