xml配置
優勢:集中配置,代碼配置分離更加方便管理
劣勢:繁雜,編譯期不容易發現錯誤
javaConfig配置
優勢:代碼簡潔,
劣勢:國內xml配置方式比較多,不容易被人接受
最簡單的spring xml配置,但是使用javaConfig只需要@Configuration一個注解. 而其中的<bean>標簽作用類似於下面的@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-beans-4.0.xsd http://www.springframework.org/schema/context"> <bean id="cat" class="com.spmjava.model.cat/> </beans>
使用步驟:
1.配置類上寫上注解@Configuration標識這個類是一個配置類
2.通過ComponentScan掃描裝載自定義Bean(bean上得配置有@Component等注解)
@ComponentScan(basePackages = {"com.spmjava.model"}) @Configuration public class PersonConfig { }
@Component public class Person{ }
3.使用@Bean注解創建第三方bean,創建的bean id是getCat
4.在web.xml配置