1.首先按照how2j教程搭建Maven项目:http://how2j.cn/k/maven/maven-eclipse-maven-project/1332.html

2. 刚建好时没有资源文件夹的,需要自己添加

    -- 之前

      Eclipse+Maven+Spring

    -- 添加

      项目右键 -> new -> Source Folder

      Eclipse+Maven+Spring

    -- 之后

      Eclipse+Maven+Spring

 

3. 导入Spring依赖

   Eclipse+Maven+Spring

    

    导入spring-context后,会自动导入其他Jar包(pom文件的dependency hierarchy视图)

    Eclipse+Maven+Spring

 

    或者看工程目录中,maven dependencies

      Eclipse+Maven+Spring

 

 

4. 编写Bean

  -- 工程目录

    Eclipse+Maven+Spring

  -- 代码

package com.atguigu.spring_1;

public class HelloBean {

    public void test() {
        System.out.println("hello, bean");
    }
}

 

5.利用

  src/main/resources目录右键,新建

  Eclipse+Maven+Spring

  Eclipse+Maven+Spring

  Eclipse+Maven+Spring

  一路Next,默认加载spring相关命名空间

 

  -- 编写xml文件(能体会到插件的好处,class自动提示全限定名!)

<?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">

    <!-- 配置HelloBean -->
    <bean id="helloBean" class="com.atguigu.spring_1.HelloBean"></bean>
    
</beans>

 

5. 应用咯

  -- 工程目录图

    Eclipse+Maven+Spring

  -- 代码

package com.atguigu.spring_1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        
        HelloBean bean = (HelloBean)ctx.getBean("helloBean");
        
        bean.test();
    }
}

 

6. 结果

   Eclipse+Maven+Spring

 

相关文章:

  • 2021-10-11
  • 2021-05-22
  • 2022-01-13
  • 2022-12-23
  • 2022-12-23
  • 2021-12-16
  • 2021-12-08
猜你喜欢
  • 2021-11-18
  • 2021-09-07
  • 2021-07-19
  • 2021-05-20
  • 2022-12-23
  • 2021-12-23
相关资源
相似解决方案