spring annotation 之 自動裝配

參考來源:http://elf8848.iteye.com/blog/442806 

1,首先修改spring配置文件:

<?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-3.0.xsd

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

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

<context:annotation-config/> 這條語句的作用,解釋地址:http://mushiqianmeng.blog.51cto.com/3970029/723880 

<context:component-scan base-package="com"/> spring會自動掃描com下面有註解的類,自動裝配bean

2classpath中加入common-annotations.jar 包

3

》自動裝配類成員變量【不需要get set方法 ,也可以寫在set方法上面】

@Autowired

@Qualifier("sessionFactory")//按名稱裝配,默認是按類型裝配

public SessionFactory sessionFactory;

》定義Bean的註解

@Controller

@Controller("Bean的名稱")

定義控制層Bean,Action

 

@Service          

@Service("Bean的名稱")

定義業務層Bean

 

@Repository   

@Repository("Bean的名稱")

定義DAOBean

 

@Component  

定義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"

     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.0.xsd

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

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

   <context:annotation-config/>

   <context:component-scan base-package="com"/> 

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

<property name="driverClassName" value="com.mysql.jdbc.Driver">

</property>

<property name="url" value="jdbc:mysql://localhost:3306/map"></property>

<property name="username" value="root"></property>

<property name="password" value="abc"></property>

</bean>

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource">

<ref bean="dataSource" />

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">

org.hibernate.dialect.MySQLDialect

</prop>

</props>

</property>

</bean>

</beans>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

<package name="test" extends="struts-default">

<action name="*_*" class="{1}" method="{2}">

<result name="success">/pages/success.jsp</result>

</action>

</package>

</struts>    

dao

package com.dao.impl;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.stereotype.Repository;

@Repository("testDaoImpl")

public class TestDaoImpl {

@Autowired

@Qualifier("sessionFactory")

public SessionFactory sessionFactory;

public Session session;

public void testDaoImplMethod(){

System.out.println("testDaoImplMethod execute ");

session=sessionFactory.openSession();

System.out.println("sessionFactory:"+sessionFactory);

System.out.println("session: "+session);

}

}

Action

@Controller("testAction")//這個是beanname struts.xml中,actionclass即是引用的bean 所以根據配置,訪問的時候,應該是testAction_methodname

昨天沒想到這個  笨死了 

public class TestAction extends ActionSupport{

@Autowired

public TestDaoImpl testDaoImpl;

public TestDaoImpl getTestDaoImpl() {

return testDaoImpl;

}

public String testActionMethod(){

System.out.println("testActionMethod execute");

testDaoImpl.testDaoImplMethod();

return SUCCESS;

}

}

訪問路徑:

localhost:8080/projectname//testAction_testActionMethod.action

昨天一直沒訪問對,老是報錯,具體什麼錯,忘了,呵呵

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章