最简单的Spring Demo(基于maven)

  • 演示效果

创建PersonService的接口和实现类,创建WeatherService的接口和实现类,PersionService实现类依赖WeatherService,PersonService有goToSchool的接口,实现这个接口的实现会调用WeatherService的ifRain,如果不会下雨就输出去上学,创建ApplicationContext容器,并且通过该容器获取person实现类的bean,并且调用goToSchool方法。

  • 引入相关包

    <properties>
        <springframework.version>4.0.2.RELEASE</springframework.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
        </dependency>
    </dependencies>

在pom文件中添加dependency。本文只需要引入spring-context的包,spring-context包已经依赖了spring-core和spring-beans的包。

  • 使用xml的配置方式

Java
public interface WeatherService {
    boolean ifRain();
}

public class WeatherServiceImpl implements WeatherService{
    @Override
    public boolean ifRain() {
        return false;
    }
}

public interface PersonService {
    void goToSchool(String name);
}

public class PersonServiceImpl implements PersonService {

    private WeatherService weatherService;
    //在这里需要set,否则xml中无法注入
    public void setWeatherService(WeatherService weatherService) {
        this.weatherService = weatherService;
    }

    @Override
    public void goToSchool(String name) {
        if (weatherService.ifRain()) {
            System.out.println("because rain");
            return;
        }
        System.out.println(name + " go to school");
    }
}
applicationContext.xml 配置文件
<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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="weatherService" class="com.huangzp.test.service.impl.WeatherServiceImpl"/>
   <!-- 在 personService中注入weatherService -->
    <bean id="personService" class="com.huangzp.test.service.impl.PersonServiceImpl">
        <property name="weatherService" ref="weatherService"/>
    </bean>
</beans>
测试
    public static void main(String[] args) {
        //创建容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取personService的bean
        PersonService personService = (PersonService)applicationContext.getBean("personService");
        //调用方法
        personService.goToSchool("huang");
    }
  • 使用xml + 注解的方式

Java
public interface WeatherService {
    boolean ifRain();
}
@Service("weatherService")
public class WeatherServiceImpl implements WeatherService{

    @Override
    public boolean ifRain() {
        return false;
    }
}
public interface PersonService {
    void goToSchool(String name);
}
@Service("personService")
public class PersonServiceImpl implements PersonService {

    @Resource
    private WeatherService weatherService;

    @Override
    public void goToSchool(String name) {
        if (weatherService.ifRain()) {
            System.out.println("because rain");
            return;
        }
        System.out.println(name + " go to school");
    }
}
applicationContext.xml 配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 如果使用注解 + xml的方式必须加此配置,表示开启扫描注解,扫描范围为com.huangzp.test下的文件-->
    <context:component-scan base-package="com.huangzp.test"/>
</beans>
测试
    public static void main(String[] args) {
        //创建容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取personService的bean
        PersonService personService = (PersonService)applicationContext.getBean("personService");
        //调用方法
        personService.goToSchool("huang");
    }
  • 使用纯注解
Java
public interface WeatherService {
    boolean ifRain();
}

public class WeatherServiceImpl implements WeatherService{
    @Override
    public boolean ifRain() {
        return false;
    }
}

public interface PersonService {
    void goToSchool(String name);
}

public class PersonServiceImpl implements PersonService {

    private WeatherService weatherService;
    //在这里需要set,否则xml中无法注入
    public void setWeatherService(WeatherService weatherService) {
        this.weatherService = weatherService;
    }

    @Override
    public void goToSchool(String name) {
        if (weatherService.ifRain()) {
            System.out.println("because rain");
            return;
        }
        System.out.println(name + " go to school");
    }
}

@Configuration
public class BeanConfig {

    @Bean(name = "personService")
    public PersonService personService() {
        PersonServiceImpl personService = new PersonServiceImpl();
        personService.setWeatherService(weatherService());
        return personService;
    }

    @Bean(name = "weatherService")
    public WeatherService weatherService() {
        return new WeatherServiceImpl();
    }
}
测试
    public static void main(String[] args) {
        //创建容器
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
        //获取personService的bean
        PersonService personService = (PersonService)applicationContext.getBean("personService");
        //调用方法
        personService.goToSchool("huang");
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,734评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,291评论 6 342
  • 1.1 spring IoC容器和beans的简介 Spring 框架的最核心基础的功能是IoC(控制反转)容器,...
    simoscode阅读 6,863评论 2 22
  • Spring Web MVC Spring Web MVC 是包含在 Spring 框架中的 Web 框架,建立于...
    Hsinwong阅读 22,984评论 1 92
  • 我一直在想今天该写点什么好呢 既然昨天定下了每天都要写作这个目标,总不能第二天就打脸吧 于是,上班的空隙浏览了一遍...
    月见笙阅读 335评论 0 0

友情链接更多精彩内容