ioc依賴注入 三種方式
1.構造方法
<bean id="person1" class="com.lsl.entity.Person">
<constructor-arg name="name" value="張三"/> 找到構造方法注入
<constructor-arg name="age" type="java.lang.Integer" value="123"/>
<constructor-arg name="context" value="哈哈"/>
</bean>
2.setter 方法
<bean id="employeeBiz" class="cn.bdqn.jboa.biz.impl.EmployeeBiz">
<property name="name" value="haha" ></property> 找到屬性名的setter方法注入
<property name="employeeDao" ref="employeeDao"></property> ref是引用id爲employeeDao的bean
</bean>
3.p命名空間注入 ==命名空間添加xmlns:p="http://www.springframework.org/schema/p"
<bean id="person2" class="com.lsl.entity.Person" p:age="12" p:context="哈哈" p:name="123"></bean>
配置AOP實例 xmlns:aop="http://www.springframework.org/schema/aop"
<bean id="userdao" class="com.lsl.daoimpl.Userdaoimpl"></bean> 實例化一個user實現類
<bean id="userbiz" class="com.lsl.bizdaoimpl.Usersbizdaoimpl">
<property name="ud" ref="userdao"></property>
</bean>
<bean id="ba" class="com.lsl.aop.beforeApplication"></bean> 實例化前置增強方法
<aop:config> 切面
<aop:pointcut expression="execution(public void addNewUsers(com.lsl.entity.Users))" id="pointcut_ys3"/> execution(method )指定方法
<aop:advisor pointcut-ref="pointcut_ys3" advice-ref="ba" /> 通過引用前置增強方法pointcut-ref是指什麼方法需要增強 advice是指引用誰作爲增強方法
</aop:config>
aop是指面向切面編程也就是指定方法(前|後)插入
通過MethodBeforeAdvice實現前置增強
前是實現一個MethodBeforeAdvice 重寫了public void before(Method arg0, Object[] arg1, Object arg2)方法;
通過AfterReturningAdvice實現後置增強
後是實現一個AfterReturningAdvice 重寫了public void afterReturning(Object returnValue, Method method,Object[] arguments, Object target)方法;
通過MethodInterceptor實現環繞增強
後是實現一個MethodInterceptor 重寫了public Object invoke(MethodInvocation arg0)方法;
arg0.getThis(); //獲取被代理對象
arg0.getMethod(); //獲取代理方法
arg0.getArguments();//獲取方法參數
Object o = arg0.proceed(); 執行所環繞的方法
通過ThrowsAdvice實現異常拋出增強
後是實現一個MethodInterceptor 重寫了public void afterThrowing()方法;
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext2.xml"); 加載spring配置文件
Userbiz u=(Userbiz)ac.getBean("userbiz"); 通過spring得到對象 業務實現類強轉
u.addNewUsers(new Users());
註解增強
使用註解進行ioc注入
導入命名空間xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config base-package="con.lsl"/>
這個配置隱式註冊了多個對註釋進行解析處理 base-package是指定位置掃描
@Autowired 默認按類型裝配,
@Resource默認按名稱裝配,當找不到與名稱匹配的bean纔會按類型裝配。
@Autowired註解是按類型裝配依賴對象,默認情況下它要求依賴對象必須存在,如果允許null值,可以設置它required屬性爲false。如:@Autowired(required=false)
private PersonDao personDao;//用於字段上
@Autowired(request=false)
public void setPersonDao(PersonDaopersonDao) {
//用於屬性的set方法上
this.personDao = personDao;
}
@Service用於標註業務層組件、
@Controller用於標註控制層組件(如struts中的action)、
@Repository用於標註數據訪問組件,即DAO組件。
而@Component泛指組件,當組件不好歸類的時候,我們可以使用這個註解進行標註。
@Component//指定該類爲Component組件
public class HibernateUtil {
//從xml文件中尋找到名字爲sessionFactory的bean後初始化sessionFactory
@Resource(name = "sessionFactory")
private SessionFactory factory;
//該屬性可以無get/set 方法
....
}
@Repository//指定該類爲Repository組件
public class PersonDaoBean implements PersonDao {
//即默認取字段的名稱作爲bean名稱尋找依賴對象,即hibernateUtil @Resource
private HibernateUtil hibernateUtil;....
}
//業務層/Service層
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.test.dao.PersonDao;
import com.test.service.PersonService;
@Service//指定該組件爲Service組件
public class PersonServiceBean implements PersonService {
@Resource//即默認取字段的名稱作爲bean名稱尋找依賴對象
private PersonDao personDao;
....
}
//控制層/Controller層
@Controller//指定該組件爲Controller組件
public class AccountMngAction
{
//即默認取字段的名稱作爲bean名稱尋找依賴對象
@Resource
private CheckInBiz checkInBiz;
@Resource
private RoomBiz roomBiz;
@Resource
private AccountBiz accountBiz;
....
}
IOC (依賴注入) 與 AOP (面向切面)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.