美文网首页
最简单的Spring Demo(基于maven)

最简单的Spring Demo(基于maven)

作者: 黄二的NPE | 来源:发表于2018-07-21 14:20 被阅读11次
    • 演示效果

    创建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");
        }
    

    相关文章

      网友评论

          本文标题:最简单的Spring Demo(基于maven)

          本文链接:https://www.haomeiwen.com/subject/rhhhmftx.html