xml配置文件的形式 VS 配置类的形式
生活随笔
收集整理的這篇文章主要介紹了
xml配置文件的形式 VS 配置类的形式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
基于xml的形式定義Bean的信息
<?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-/定義一個Bean的信息<bean id="car" class="com.leon.compent.Car"></bean> </beans>去容器中讀取Bean
public static void main( String[] args ) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); System.out.println(ctx.getBean("person")); }基于讀取配置類的形式定義Bean信息
@Configuration public class MainConfig {@Beanpublic Person person(){return new Person();} }注意: 通過@Bean的形式是使用的話, bean的默認名稱是方法名,若@Bean(value="bean的名稱")那么bean的名稱是指定的
去容器中讀取Bean的信息(傳入配置類)
public static void main( String[] args ) {AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class);System.out.println(ctx.getBean("person")); }?
總結
以上是生活随笔為你收集整理的xml配置文件的形式 VS 配置类的形式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring框架功能整体介绍
- 下一篇: 在配置类上写@CompentScan注解