spring註解
@controller(給web層的註解)
@service(給serivce層加的註解)
@repository(給dao層加的註解)
@component(給java類加註解,老版本spring只有這一個註解)
以上四個註解:將對應的類納入spring容器中對應的
Id:類名第一個字母小寫(默認)
如果需要自己指定id需要給三個註解加入String類的參數
@controller(“uAction”) id=uAction
@resouce(給需要依賴的對象屬性加的註解)
通過自動裝配完成需要依賴屬性的注入。
參數:name:按照byName進行自動裝配
參數:type:按照byType進行自動裝配
創建一個配置類,在配置類上添加 @ComponentScan 註解。該註解默認會掃描該類所在的包下所有的配置類,相當於之前的 <context:component-scan>
指定要掃描的包(使用@ComponentScan 的 valule 屬性來配置)
@ComponentScan(value = "io.mieux.controller")
如果使用的 jdk8,則可以直接添加多個 @ComponentScan 來添加多個掃描規則,但是在配置類中要加上 @Configuration 註解,否則無效。
@Configuration用於定義配置類,可替換xml配置文件,被註解的類內部包含有一個或多個被@Bean註解的方法
@Configuation等價於<Beans></Beans>
@Bean等價於<Bean></Bean>
@ComponentScan等價於<context:component-scan base-package=”com.dxz.demo”/>
配合@Configuration使用,包括 @EnableAsync, @EnableScheduling, @EnableTransactionManagement, @EnableAspectJAutoProxy, @EnableWebMvc。
@Configuration
@ComponentScan("com.fushikan")
@Import({JdbcConfig.class,TransactionConfig.class})
@PropertySource("db.properties")
@EnableTransactionManagement
public class SpringConfiguration {
}
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
@ContextConfiguration(locations= "classpath:bena.xml")
springmvc註解
springMVC 開發環境搭建
1.pom文件導入jar包座標依賴
2在web.xml中配置前端控制器
3.在resource中創建springmvc.xml配置文件
@RequestMapping(path=“/hello”)訪問路徑