目录
方式二:修改Spring配置类,设定扫描范围为com.itheima,排除掉controller包中的bean
编写Controller,Service,Dao,Domain类
加载Spring控制的bean的时候排除掉SpringMVC控制的bean
具体该如何排除:
方式一:Spring加载的bean设定扫描范围为精准范围,例如service包、dao包等
方式二:Spring加载的bean设定扫描范围为com.itheima,排除掉controller包中的bean
方式三:不区分Spring与SpringMVC的环境,加载到同一个环境中[了解即可]
- @Configuration
- @ComponentScan({"com.itheima.service","comitheima.dao"})
- public class SpringConfig {
- }
说明:
上述只是通过例子说明可以精确指定让Spring扫描对应的包结构,真正在做开发的时候,因为Dao最终是交给MapperScannerConfigurer
对象来进行扫描处理的,我们只需要将其扫描到service包即可。
- @Configuration
- @ComponentScan(value="com.itheima",
- excludeFilters=@ComponentScan.Filter(
- type = FilterType.ANNOTATION,
- classes = Controller.class
- )
- )
- public class SpringConfig {
- }
excludeFilters属性:设置扫描加载bean时,排除的过滤规则
type属性:设置排除规则,当前使用按照bean定义时的注解类型进行排除
ANNOTATION:按照注解排除
ASSIGNABLE_TYPE:按照指定的类型过滤
ASPECTJ:按照Aspectj表达式排除,基本上不会用
REGEX:按照正则表达式排除
CUSTOM:按照自定义规则排除
大家只需要知道第一种ANNOTATION即可
classes属性:设置排除的具体注解类,当前设置排除@Controller定义的bean
将pom.xml中多余的内容删除掉,再添加SpringMVC需要的依赖
- "1.0" encoding="UTF-8"?>
-
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <groupId>com.itheimagroupId>
- <artifactId>SpringMVCartifactId>
- <version>1.0-SNAPSHOTversion>
- <packaging>warpackaging>
-
- <name>SpringMVC Maven Webappname>
-
- <url>http://www.example.comurl>
-
- <properties>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <maven.compiler.source>1.8maven.compiler.source>
- <maven.compiler.target>1.8maven.compiler.target>
- properties>
-
- <dependencies>
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>3.1.0version>
- <scope>providedscope>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>5.2.10.RELEASEversion>
- dependency>
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druidartifactId>
- <version>1.1.16version>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>3.5.6version>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>5.1.47version>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-jdbcartifactId>
- <version>5.2.10.RELEASEversion>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>1.3.0version>
- dependency>
- dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.tomcat.mavengroupId>
- <artifactId>tomcat7-maven-pluginartifactId>
- <version>2.1version>
- <configuration>
- <port>80port>
- <path>/path>
- configuration>
- plugin>
- plugins>
- build>
-
- project>
- package com.itheima.config;
-
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
-
- @Configuration
- @ComponentScan("com.itheima.controller")
- public class SpringmvcConfig {
- }
-
-
- public interface UserDao {
- @Insert("insert into tbl_account(name,money)values(#{name},#{money})")
- public void save(User user);
- }
-
-
-
-
- public class User {
- private Integer id;
- private String name;
- private Integer age;
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
-
- @Override
- public String toString() {
- return "User{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- }
-
-
-
-
- public interface UserService {
- public void save(User user);
- }
-
-
- @Service
- public class UserServiceImpl implements UserService {
- public void save(User user) {
- System.out.println("user service ...");
- }
- }
- package com.itheima.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- //@Controller:Springmvc的bean标志
- @Controller
- public class UserController {
- @RequestMapping("/save")
- @ResponseBody
- public String save(){
- System.out.println("郭浩康第一个Sprintmvc项目创建成功");
- return "{spring1save()...}";
- }
- }
- @Configuration
- //排除扫描某些包
- @ComponentScan(value = "com.itheima",
- excludeFilters = @ComponentScan.Filter(
- type = FilterType.ANNOTATION,
- classes = Controller.class
- ))
- public class SpringConfig {
- }
将web.xml删除,换成ServletContainersInitConfig
- package com.itheima.config;
-
- import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
-
- public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
-
- @Override
- protected Class>[] getRootConfigClasses() {
- return new Class[]{SpringConfig.class};
- }
-
- @Override
- protected Class>[] getServletConfigClasses() {
- return new Class[]{SpringmvcConfig.class};
- }
-
- @Override
- protected String[] getServletMappings() {
- return new String[]{"/"};
- }
- }
- //public class ServletConfig extends AbstractDispatcherServletInitializer {
- // //加载Springmvc容器
- // @Override
- // protected WebApplicationContext createServletApplicationContext() {
- // AnnotationConfigWebApplicationContext ctx=new AnnotationConfigWebApplicationContext();
- // ctx.register(SpringmvcConfig.class);
- // return ctx;
- // }
- 设置请求处理
- // @Override
- // protected String[] getServletMappings() {
- // return new String[]{"/"};
- // }
- 加载Spring容器
- // @Override
- // protected WebApplicationContext createRootApplicationContext() {
- // return null;
- // }
- //}
- public class APP {
- public static void main (String[] args){
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
- System.out.println(ctx.getBean(UserController.class));
- }
- }