配置文件的命名空间


Spring的注解机制,是类加载时读入配置文件的,即修改了关于注解的配置,如果不重启服务,那么配置文件就不会读取到注解的更新
@Service("serviceUser")
相当于 <bean id="serviceUser" class="com.yg.testdangdang.service.impl.ServiceUserImpl"></bean>
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ServiceUser serviceUser = (ServiceUser)context.getBean("serviceUser");
由Spring创建的对象,才可以享有Spring的功能,比如自动装配的对象,如果new了一个新对象,那么就没有自动装配的功能了
BaseJunit4Test:
@RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试
@ContextConfiguration(locations={"classpath:applicationContext.xml"})//加载配置文件
public class test {
@Autowired
private UserService userService;
@Test
public void test(){
System.out.println(userService);
}
}
<aop:pointcut expression="@annotation(com.yg.testdangdang.annotation.Log)" id="myPointcut"/>
定义切入点为注解,Log,需要提前创建这个软接口
@Target({ElementType.PARAMETER,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
}
<aop:advisor advice-ref="myBeforeAdvice" pointcut-ref="myPointcut"/>
@Log 即有这个注解的,才实现myPointcut增强,
public boolean login(String userEmail,String userPassword) {}
@RequestMapping("/user")
public class UserController
在类名上面加RequestMapping注解,表示该类添加了命名空间。即所有的返回值加“/user”
return "forward:/main/main.jsp"
如果其中一个想例外,那么就要加forward(转发),redirect(重定向)关键字,表示不经过逻辑视图,并且前面要加一个“/”,来组合成绝对路径
Spring的配置文件,扫描路径是包路径,不是类路径
Dao层:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yg.testcmfz.dao"></property>
</bean>
Service层:
<context:component-scan base-package="com.yg.testcmfz.service"></context:component-scan>
@Service配置在实现类,base-package指定接口的包路径
Spring+Struts2+MyBatis 整合
原在controller层使用SpringMVC的注解来提供service对此昂,现在由于使用了struts2,没有了注解了,那么就将url功能放在applicationContext上实现bean的创建,bean指出service的全路径,bean ID交给struts.xml 的class
<action name="*Book" class="bookAction" method="{1}">
整合后,Spring来创建对象,Struts2来控制流程的跳转
applicationContext配置文件bean的调用流:
没有了springMVC的注解,userAction要持有userService对象的引用

在userAction设置userService的get,set方法

userService要持有userDao对象的引用

在userServiceImpl设置userDao的get,set方法

由于dao层是接口,因此需要添加mapperInterface参数,value制定dao层的实际位置;
整合MyBatis,需要持有sqlSessionFactory的引用

sqlSessionFactory持有dataSource的引用,并配置指明*DaoMapper.xml的实际路径

dataSource配置数据源

从userAction到dataSource,bean被层层调用,实际底层是靠:
- Set,Get 调用对象
- 无参构造方法和反射创建对象
assertNotNull 断言
- public User getUser(String username) {
- if (username == null || username.trim().length() != 0)
- throw new IllegalArgumentException("username is invalid,invalid username:" + username);
- //.......省去
- }
=
- public int getUser(String username) {
- Assert.hasText(username);//这里没有使用自定义的错误描述,而是使用默认的错误描述,
- }
select d1.category_id,d1.category_name,d1.parent_id,d1.count,
d2.category_id category_id2,d2.category_name category_name2,d2.parent_id parent_id,d2.count count2 from
dangstore_category d1 inner join dangstore_category d2
on d1.category_id = d2.parent_id
使用内连接时,在select后需要给别名,不然java无法分辨正确的字段名,不可以使用d1.*,d2.*
Web.xml 加载顺序
在java web规范中说明.按照listner,filter,servlet的顺序,从上至下初始化.
SpringMVC 异常处理
http://blog.youkuaiyun.com/goodyuedandan/article/details/62420120
推荐使用 实现HandlerExceptionResolver 接口自定义异常处理器
Controller方法内的try捕获异常 优于 全局异常捕获
public class MyExceptionResolver implements HandlerExceptionResolver{
@Override
public ModelAndView resolveException(HttpServletRequest req,
HttpServletResponse resp, Object o, Exception ex) {
//Object是抛出异常的引发者,调用方法
//Exception就是那个剖出的异常
System.out.println("全局捕获异常");
//判断这个异常属于那个异常类
if (ex instanceof UnauthorizedException){
//打印异常内容信息
System.out.println(ex);
}
//向前台返回错误信息
ModelAndView modelAndView = new ModelAndView();
//将错误信息存入session
modelAndView.addObject("error_message", ex);
//跳转页面
modelAndView.setViewName("/jsp/error");
return modelAndView;
}
}
本文详细介绍了Spring框架的配置方式,包括注解配置、XML配置等,并深入探讨了@Service、@Autowired等注解的作用及使用场景,同时讲解了Spring与MyBatis、Struts2的整合方法。
406

被折叠的 条评论
为什么被折叠?



