1.mybatis测试,使用junit进行测试,junit显示有错但控制台仅显示警告
以控制台为准,原因为导入了无用的包。无需理会。
2.spring的初入门配置
导入jar包,添加junit包,设置配置文件。当然要在eclipse的xml中添加xsd文档,类型为schema location
spring的初始配置,在applicationContext.xml文件中利用bean为类配置对象,为bean注入属性并赋值。
当属性为bean时,可以使用ref引入一个bean赋值,也可以使用default-autowire="byName"自动装配赋值
所有的属性均需要setter方法
<?xml version="1.0" encoding="UTF-8"?> <bean id="user" class="pojo.User">
<property name="id" value="5"/>
<property name="orders" ref="orders"/>
</bean>
<bean id="orders" class="pojo.Orders">
<property name="id" value="6"/>
</bean>
<!--
<bean id="userDao" class="dao.impl.UserDaoImpl"/> 配置三层 一种方法
<bean id="userService" class="service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
<bean id="userServlet" class="servlet.UserServlet">
<property name="userService" ref="userService"/>
</bean> -->
<!---配置三层,自动装配第2种方法-->
<bean id="userDao" class="dao.impl.UserDaoImpl"/>
<bean id="userService" class="service.impl.UserServiceImpl">
</bean>
<bean id="userServlet" class="servlet.UserServlet">
</bean>
测试:
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.User;
import servlet.UserServlet;
public class Test {
public static void main(String[] args) {
String path="applicationContext.xml";
ApplicationContext spring = new ClassPathXmlApplicationContext(path);
UserServlet user = (UserServlet) spring.getBean("userServlet");
user.say();
}
}
3.spring的进阶
spring的配置文件中,必须自动扫描需要注解生成的包。扫描后的包的类,才能通过注解生成对象。
使用注解生成,他会自动装配
<context:component-scan base-package="dao,service,servlet">
</context:component-scan>
</beans>
注解生成bean的类中,所有的属性包括类属性,必须通过@Autowired注解加入bean.类中的属性不需要setter方法
bean中的所有方法不需要加注解,除了初始化方法@postConstruct,销毁方法preDestroy
接口实现类的对象会自动赋值给接口对象,但当接口有多个实现类时,需要使用@Qualifier(“service”)区分使用
哪一个实现类的对象。
测试类:
package test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import servlet.UserServlet;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations=“classpath:/applicationContext.xml”)
public class Test {
@Autowired
UserServlet userServlet;
@org.junit.Test
public void testRun() {
userServlet.say();
}
}
4.AOP很重要,作为代理类用于在不改变源程序的情况下对程序进行进行拓展,使整个程序代码结构更加清晰
流程:导入代理类jar、加载xsd文件,配置aop、配置切面、配置切点、配置通知方法
注意点:切面必须是一个bean,通知方法有5种,都是切面类里面的方法,切点中包含的所有方法均是与通知方法相关。
非注解中的配置:
<aop:config>
<aop:aspect ref="add" >
<aop:pointcut expression="execution(* servlet.*.*(..))" id="pt"/>
<aop:after method="B" pointcut-ref="pt"/>
<aop:before method="A" pointcut-ref="pt"/>
</aop:aspect>
</aop:config>、
注解中的配置:
xml文件中加入aop:aspectj-autoproxy/aop配置,@component与@Aspect注解切面类,配置切点,配置通知方法
1.
@Component
@org.aspectj.lang.annotation.Aspect
public class Aspect {
@Before(value = "servlet.UserServlet.say()")
public void sayBefore() {
System.out.println("说话之前");
}
@After(value = "servlet.UserServlet.say()")
public void sayAfter() {
System.out.println("说话之后");
}
@Around(value = "servlet.UserServlet.say()")
public void sayRound() {
System.out.println("围绕话题");
}
}
2.切点的配置
@Controller
public class UserServlet {
@Autowired
UserService userService;
//该pointcut用来拦截test包下的所有类中的所有方法。
@Pointcut(value = "within(servlet.*)")
public void say() {
System.out.println("执行了servlet层");
userService.say();
}
}