@Component;@Controller;@Service;@Repository
在annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spring容器管理的类。即就是该类已经拉入到spring的管理中了。而@Controller, @Service, @Repository是@Component的细化,这三个注解比@Component带有更多的语义,它们分别对应了控制层、服务层、持久层的类。
@Repository标签是用来给持久层的类定义一个名字,让Spring根据这个名字关联到这个类。
例如:
@Repository("userDao")
public class UserDaoImpl implements UserDao{
........................................
}
声明了UserDaoImpl 在Spring容器中叫userDao这个名字。
@Service是用于服务层的IServiceImpl类文件,功能与@Repository类似。
另外标签:@Autowired 用来注入。
例如:
@Autowired
private UserDao userDao;
这样就注入进去了,相当于我们new了个实现类,我们就无需写setter方法了。
我们还得有配置文件进行配置:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.zxr.manager">
<context:include-filter type="regex" expression=".*DaoImpl"/>
<context:include-filter type="regex" expression=".*ServiceImpl"/>
</context:component-scan>
</beans>
这样就把com.zxr.manager包下的所有.*DaoImpl,.*ServiceImpl都注册关联到Spring容器中去了。
1 问题:有多个 @RequestMapping
@controller
@RequestMapping("/aaa") ----------------->类级别 可以不需要 如果要了 下面所有的请求路径前 都需要加入 /aaa
public class ccccontroller{
@RequestMapping("/bbb") --------------------->方法级别 必须有 决定这个方法处理哪个请求 如果有类级别 /aaa/bbb
public String xxx(){ 如果没有 /bbb
retrun
}
}
2:问题:接收用户请求参数 值
1)请求1: /test/start.do?name=zhangsan
请求2: /test/start/zhangsan.do
在请求2中 将参数作为请求URL 传递 感觉用的不习惯
采用 URL模板
2)@RequestMapping("/start/{name}") 这个name 随便 啥都可以
public String start(@PathVariable("name") string name){ 反正和上面的对应
return 方法体里面就可以直接获得参数
}
3)包含多个 @RequestMapping ("/start/{name}/{age}")
3 问题 不同请求方法 用不同处理方法 get post
@RequestMapping (value="/start" ,method=RequestMethod.GET) 处理post 就换成 POST