IDEA中利用Maven创建Web项目、为Web应用添加Spring框架支持、bean的创建于获取

 

1 环境版本说明

  Jdk : 1.8

  Maven : 3.5 

  IDEA : 专业版 2017.2

 

2 环境准备

  2.1 Maven安装及其配置

  2.2 Tomcat安装及其配置

 

3 详细步骤

  3.1 根据模板创建maven工程

    file -> new -> project -> maven -> webapp

    技巧01:根据模板创建web工程时,请选择 maven-archetype-webapp 

  3.2 目录结构调整

    项目创建成功后的目录结构如下:

    跳坑01:新创建的项目中没有存放源文件的java文件夹,也没有存放测试文件的test文件夹,同样没有存放资源文件的resources文件夹

    跳坑01:在main目录下新建java、resources两个文件夹,分别用来存放源文件和资源文件;在main的同级目录中新建test目录用来存放测试文件夹

    技巧01:虽然我们创建了相关的文件夹,但是IDEA并不知道java文件夹是用来存放源文件,test用来存放测试文件,resources用来存放资源文件的;我们必须进行手动配置:

      file -> project structure -> modules

    设置完后整个目录结构如下:

  3.3 配置tomcat

    3.3.1 打开启动配置页面

    3.3.2 添加tomcat启动项

    3.3.3 配置tomcat基本信息

    3.3.4 添加web模块

      技巧01:为项目添加一个web模块,file -> project structure -> module 

      跳坑01:利用IDEA创建项目时会在main目录下创建一个webapp文件夹,该文件夹里面的内容就是需要被部署到tomcat容器的内容,但是我们为项目添加了web模块后会自动在项目的根目录下生成一个web文件夹【建议将这个web文件夹删掉】,这个文件夹的作用和main目录下的webapp文件夹的作用相同,而且添加web模块时自动寻找的是新创建的web文件夹下面的web.xml文件;将web.xml改成webapp下面的web.xml,并将web的源文件文件夹改成webapp,修改后的效果如下:

    3.3.5 添加artifacts

      技巧01:添加一个web应用,这个web引用来源于modules【其实就是来源于我们创建的web工程】

    3.3.6 配置发布页面

      将 artifacts 中配置为web应用添加到tomcat配置中的deployment

 

    3.3.7 配置开发热部署

      就是修改前后台代码后不用重启tomcat,IDEA会自动进行【修改后台时自动重启tomcat服务器,修改前台时刷新浏览器就可以啦】

    3.3.8 启动测试

      直接通过IDEA启动tomcat就可以啦

      技巧01:应用启动成功后,会自动访问webapp里面的index.jsp页面

 

4 添加框架支持

  我们创建的Web应用知识一个架子,不过IDEA支持自动添加框架;这样就不需要手动在pom.xml中添加相关框架的依赖配置了

  右键项目 -> add framework stupport 

  技巧01:本博文主要添加spring框架的支持

  技巧02:点击确认后会自动将spring框架的依赖包下载到项目中去【PS: 是直接将依赖下载到项目中的lib目录下】,整个过程有点花时间

  跳坑01:如果下载依赖期间由于网络原因失败,这时候就需要重新添加框架;但是这时候发现已经没有spring相关的选项了

  填坑01:这是后就需要进入到项目结构中的modules配置中,将spring相关的模块删除,在重新进行框架添加

  技巧03:添加完spring框架支持后会在webapp文件夹下自动生成相关的配置文件,并在webapp中的web.xml中对这些配置文件记性监听配置

 

5 Bean相关

  Bean相关的详细内容请参见《精通spring4.x企业应用开发实战》

  5.1 配置bean

    5.1.1 准备

      在pom.xml文件中引入lombok依赖

      创建Student类和Teacher类

package domain;

import lombok.Data;

/**
 * @author 王杨帅
 * @create 2018-08-10 20:43
 * @desc 學生實體類
 **/
@Data
public class Student {

    private String id;
    private String name;
    private Integer age;
    private String address;

    public void info() {
        System.out.println("我是學生,我在學習控制反轉相關知識點。");
    }

}
Student.java
package domain;

/**
 * @author 王杨帅
 * @create 2018-08-10 20:45
 * @desc 老師實體類
 **/
public class Teacher {

    private String id;
    private String name;
    private Integer age;
    private String address;

    public void info() {
        System.out.println("我是老师,我在教授IOC相关知识点。");
    }

}
Teacher.java
    5.1.2 利用xml配置

      技巧01:需要将配置文件放到resources文件夹下【之前通过添加spring框架支持时产生的配置文件位于webapp下面,移动后需要更改web.xml配置文件???待修改】

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="student" class="domain.Student"></bean>

</beans>
View Code
    5.1.3 利用类注解配置
package core.config;

import domain.Teacher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 王杨帅
 * @create 2018-08-10 20:59
 * @desc bean配置类
 **/
@Configuration
public class Beans {

    @Bean(value = "teacher")
    public Teacher buildTeacher() {
        return new Teacher();
    }

}
Beans.java

  5.2 获取bean

     5.2.1 利用BeanFactory获取
    /**
     * 利用BeanFactory获取bean
     * @throws IOException
     */
    @Test
    public void test01() throws IOException {
        System.out.println("Hello Boy");
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource resource = resolver.getResource("classpath:applicationContext.xml");

        System.out.println(resource.getURL());

        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
        reader.loadBeanDefinitions(resource);

        System.out.println("初始化BeanFactory完毕");

        Student student = factory.getBean("student", Student.class);
        System.out.println("student bean 获取成功");

        student.info();

    }
View Code
    5.2.2. 利用ApplicationContext获取

      技巧01:利用ApplicationContext获取xml配置的bean和配置类配置的bean需要用不同的实现类

    /**
     * 利用ApplicationContext獲取bean
     */
    @Test
    public void test02() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Student student = applicationContext.getBean("student", Student.class);

        student.info();
    }

    /**
     * 利用ApplicationContext获取配置类配置的bean
     */
    @Test
    public void test03() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Beans.class);
        Teacher teacher = applicationContext.getBean("teacher", Teacher.class);

        teacher.info();

    }
View Code
    5.2.3 获取bean代码汇总
package domain;

import core.config.Beans;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;

/**
 * @author 王杨帅
 * @create 2018-08-10 20:47
 * @desc 测试类
 **/
public class TestDemo {

    @Before
    public void init() {
        System.out.println("初始化方法");
    }

    /**
     * 利用BeanFactory获取bean
     * @throws IOException
     */
    @Test
    public void test01() throws IOException {
        System.out.println("Hello Boy");
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource resource = resolver.getResource("classpath:applicationContext.xml");

        System.out.println(resource.getURL());

        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
        reader.loadBeanDefinitions(resource);

        System.out.println("初始化BeanFactory完毕");

        Student student = factory.getBean("student", Student.class);
        System.out.println("student bean 获取成功");

        student.info();

    }

    /**
     * 利用ApplicationContext獲取bean
     */
    @Test
    public void test02() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Student student = applicationContext.getBean("student", Student.class);

        student.info();
    }

    /**
     * 利用ApplicationContext获取配置类配置的bean
     */
    @Test
    public void test03() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Beans.class);
        Teacher teacher = applicationContext.getBean("teacher", Teacher.class);

        teacher.info();

    }

}
View Code

    

6 本博文参考源代码

  点击下载源代码

 

7 参考博文

  项目创建 

  添加框架依赖

 

 

      

    

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 创建Maven项目IDEA选择File -> New -> Project,选择Maven,点击Next。 在Group Id和Artifact Id填写项目信息,点击Next。 选择项目保存路径和项目名称,点击Finish。 2. 导入Spring框架 打开pom.xml文件,在<dependencies>标签添加以下代码: ```xml <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.6.RELEASE</version> </dependency> ``` 这个示例使用的是Spring Context,还可以根据需要添加其他Spring框架的依赖。 3. 编写applicationContext.xml文件 在src/main/resources目录下创建applicationContext.xml文件。 示例代码如下: ```xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置bean --> <bean id="userService" class="com.example.UserService"> <property name="userDao" ref="userDao"/> </bean> <bean id="userDao" class="com.example.UserDao"/> </beans> ``` 这个示例配置了两个bean,userService和userDao。其userService依赖于userDao。 4. 配置文件方式实现依赖注入 在需要使用依赖注入的类,使用@Autowire注解标注需要注入的属性。 示例代码如下: ```java @Component public class UserController { @Autowired private UserService userService; } ``` 5. 注解方式实现依赖注入 在需要注入的类,使用@Component注解标注类,并使用@Autowired注解标注需要注入的属性。 示例代码如下: ```java @Component public class UserService { @Autowired private UserDao userDao; } ``` 在启动应用程序后,Spring会自动扫描所有使用@Component注解的类,并将它们实例化并注入其他需要它们的类。 6. 运行项目IDEA点击Run按钮或者使用快捷键Shift + F10运行项目。 在控制台可以看到Spring框架自动注入了需要的依赖。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值