【Java】使用Spring的JavaConfig

130 阅读4分钟
原文链接: click.aliyun.com
  1. 云栖社区>
  2. 博客列表>
  3. 正文

使用Spring的JavaConfig

泳泳啊泳泳 2018-01-08 12:33:20 浏览279 评论0

java spring 配置 string Annotation xml class schema bean

摘要: 之前我们都是在xml文件中定义bean的,比如: 1 2 3 4 5 6 7 8 <beans xmlns="http://www.springframework.org/schema/beans "     xmlns:xsi="http://www.

之前我们都是在xml文件中定义bean的,比如:


                                                                            

                                                                                                

                                                                                                    

                                                                                                    <
                                                                                                        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-3.0.xsd">
                                                                                                    

                                                                                                    

                                                                                                      

                                                                                                    

                                                                                                        
                                                                                                        <bean 
                                                                                                            id="helloBean" class="com.mkyong.hello.impl.HelloWorldImpl">
                                                                                                    

                                                                                                    

                                                                                                      

                                                                                                    

                                                                                                    </
                                                                                                        beans>                                                                                                            

                                                                                                

                                                                                            

                                                                        

其实我们可以使用注解来完成这些事情,例如下面的代码,完成的功能和上面的xml配置的功能是一样的:


                                                                            

                                                                                                

                                                                                                    

                                                                                                    import 
                                                                                                        org.springframework.context.annotation.Bean;
                                                                                                    

                                                                                                    

                                                                                                    import 
                                                                                                        org.springframework.context.annotation.Configuration;
                                                                                                    

                                                                                                    

                                                                                                    import 
                                                                                                        com.mkyong.hello.HelloWorld;
                                                                                                    

                                                                                                    

                                                                                                    import 
                                                                                                        com.mkyong.hello.impl.HelloWorldImpl;
                                                                                                    

                                                                                                    

                                                                                                      

                                                                                                    
@Configuration

                                                                                                    

                                                                                                    public 
                                                                                                        class AppConfig {                                                                                                            

                                                                                                    

                                                                                                      

                                                                                                    

                                                                                                        
                                                                                                        @Bean(name=
                                                                                                            "helloBean")                                                                                                                

                                                                                                    

                                                                                                        
                                                                                                        public HelloWorld helloWorld() {                                                                                                            

                                                                                                    

                                                                                                            
                                                                                                        return new 
                                                                                                            HelloWorldImpl();
                                                                                                    

                                                                                                    

                                                                                                        
                                                                                                        }
                                                                                                    

                                                                                                    

                                                                                                      

                                                                                                    
}

                                                                                                

                                                                                            

                                                                        

 想象一个场景,我们有一个很大的工程项目,如果将所有的bean都配置在一个xml文件中,那么这个文件就会非常的大。所以在很多的时候我们都会将一个大的xml配置文件分割为好几份。这样方便管理,最后在总的那个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-2.5.xsd">
                                                                                                    

                                                                                                    

                                                                                                      

                                                                                                    

                                                                                                        
                                                                                                        <import 
                                                                                                            resource="config/customer.xml"/>
                                                                                                    

                                                                                                    

                                                                                                            
                                                                                                        <import 
                                                                                                            resource="config/scheduler.xml"/>
                                                                                                    

                                                                                                    

                                                                                                      

                                                                                                    

                                                                                                    </
                                                                                                        beans>                                                                                                            

                                                                                                

                                                                                            

                                                                        

 但是现在我们也可以使用JavaConfig来完成同样的工作了:


                                                                            

                                                                                                

                                                                                                    

                                                                                                    import 
                                                                                                        org.springframework.context.annotation.Configuration;
                                                                                                    

                                                                                                    

                                                                                                    import 
                                                                                                        org.springframework.context.annotation.Import;
                                                                                                    

                                                                                                    

                                                                                                      

                                                                                                    
@Configuration

                                                                                                    

                                                                                                    @Import
                                                                                                        ({ CustomerConfig.class
                                                                                                            , SchedulerConfig.class 
                                                                                                                })
                                                                                                    

                                                                                                    

                                                                                                    public 
                                                                                                        class AppConfig {                                                                                                            

                                                                                                    

                                                                                                      

                                                                                                    
}

                                                                                                

                                                                                            

                                                                        

  我们对这个例子来看一个demo:

CustomerBo.java


                                                                            

                                                                                                

                                                                                                    

                                                                                                    public 
                                                                                                        class CustomerBo {                                                                                                            

                                                                                                    

                                                                                                      

                                                                                                    

                                                                                                        
                                                                                                        public void 
                                                                                                            printMsg(String msg) {
                                                                                                    

                                                                                                    

                                                                                                      

                                                                                                    

                                                                                                            
                                                                                                        System.out.println("CustomerBo : " 
                                                                                                            + msg);
                                                                                                    

                                                                                                    

                                                                                                        
                                                                                                        }
                                                                                                    

                                                                                                    

                                                                                                      

                                                                                                    
}

                                                                                                

                                                                                            

                                                                        

 SchedulerBo.java


                                                                            

                                                                                                

                                                                                                    

                                                                                                    public 
                                                                                                        class SchedulerBo {                                                                                                            

                                                                                                    

                                                                                                      

                                                                                                    

                                                                                                        
                                                                                                        public void 
                                                                                                            printMsg(String msg) {
                                                                                                    

                                                                                                    

                                                                                                      

                                                                                                    

                                                                                                            
                                                                                                        System.out.println("SchedulerBo : " 
                                                                                                            + msg);
                                                                                                    

                                                                                                    

                                                                                                        
                                                                                                        }
                                                                                                    

                                                                                                    

                                                                                                      

                                                                                                    
}

                                                                                                

                                                                                            

                                                                        

  现在我们来使用注解:


                                                                            

                                                                                                

                                                                                                    
@Configuration

                                                                                                    

                                                                                                    public 
                                                                                                        class CustomerConfig {                                                                                                            

                                                                                                    

                                                                                                      

                                                                                                    

                                                                                                        
                                                                                                        @Bean(name=
                                                                                                            "customer")                                                                                                                

                                                                                                    

                                                                                                        
                                                                                                        public CustomerBo customerBo(){                                                                                                            

                                                                                                    

                                                                                                      

                                                                                                    

                                                                                                            
                                                                                                        return new 
                                                                                                            CustomerBo();
                                                                                                    

                                                                                                    

                                                                                                      

                                                                                                    

                                                                                                        
                                                                                                        }
                                                                                                    

                                                                                                    
}

                                                                                                

                                                                                            

                                                                        

  


                                                                            

                                                                                                

                                                                                                    
@Configuration

                                                                                                    

                                                                                                    public 
                                                                                                        class SchedulerConfig {                                                                                                            

                                                                                                    

                                                                                                      

                                                                                                    

                                                                                                        
                                                                                                        @Bean(name=
                                                                                                            "scheduler")                                                                                                                

                                                                                                    

                                                                                                        
                                                                                                        public SchedulerBo suchedulerBo(){                                                                                                            

                                                                                                    

                                                                                                      

                                                                                                    

                                                                                                            
                                                                                                        return new 
                                                                                                            SchedulerBo();
                                                                                                    

                                                                                                    

                                                                                                      

                                                                                                    

                                                                                                        
                                                                                                        }
                                                                                                    

                                                                                                    

                                                                                                      

                                                                                                    
}

                                                                                                

                                                                                            

                                                                        

  AppConfig.java


                                                                            

                                                                                                

                                                                                                    

                                                                                                    import 
                                                                                                        org.springframework.context.annotation.Configuration;
                                                                                                    

                                                                                                    

                                                                                                    import 
                                                                                                        org.springframework.context.annotation.Import;
                                                                                                    

                                                                                                    

                                                                                                      

                                                                                                    
@Configuration

                                                                                                    

                                                                                                    @Import
                                                                                                        ({ CustomerConfig.class
                                                                                                            , SchedulerConfig.class 
                                                                                                                })
                                                                                                    

                                                                                                    

                                                                                                    public 
                                                                                                        class AppConfig {                                                                                                            

                                                                                                    

                                                                                                      

                                                                                                    
}

                                                                                                

                                                                                            

                                                                        

  然后运行:


                                                                            

                                                                                                

                                                                                                    

                                                                                                    public 
                                                                                                        class App {                                                                                                            

                                                                                                    

                                                                                                        
                                                                                                        public static 
                                                                                                            void main(String[] args) {                                                                                                                

                                                                                                    

                                                                                                      

                                                                                                    

                                                                                                            
                                                                                                        ApplicationContext context = new 
                                                                                                            AnnotationConfigApplicationContext(
                                                                                                    

                                                                                                    

                                                                                                                    
                                                                                                        AppConfig.class
                                                                                                            );
                                                                                                    

                                                                                                    

                                                                                                      

                                                                                                    

                                                                                                            
                                                                                                        CustomerBo customer = (CustomerBo) context.getBean("customer"
                                                                                                            );
                                                                                                    

                                                                                                    

                                                                                                            
                                                                                                        customer.printMsg("Hello 1"
                                                                                                            );
                                                                                                    

                                                                                                    

                                                                                                      

                                                                                                    

                                                                                                            
                                                                                                        SchedulerBo scheduler = (SchedulerBo) context.getBean("scheduler"
                                                                                                            );
                                                                                                    

                                                                                                    

                                                                                                            
                                                                                                        scheduler.printMsg("Hello 2"
                                                                                                            );
                                                                                                    

                                                                                                    

                                                                                                      

                                                                                                    

                                                                                                        
                                                                                                        }
                                                                                                    

                                                                                                    
}

                                                                                                

                                                                                            

                                                                        

  

 

 


============================================================================== 本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/12/27/2835087.html,如需转载请自行联系原作者
版权声明:本文内容由互联网用户自发贡献,版权归作者所有,本社区不拥有所有权,也不承担相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:yqgroup@service.aliyun.com 进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。

用云栖社区APP,舒服~

【云栖快讯】中办国办印发《推进互联网协议第六版(IPv6)规模部署行动计划》加快推进基于 IPv6 的下一代互联网规模部署,计划指出2025年末中国 IPv6 规模要达到世界第一,阿里云也第一时间宣布了将全面提供IPv6服务,那么在全面部署 IPV6 前,你需要了解都在这儿  详情请点击 评论文章 (0) (0) (0)
分享到:

相关文章

网友评论