首先注意標題,說的是類內部的註解java
結論是:web
不能,可是子類卻能夠享有父類中該註解帶來的效果。spring
看了一下這個:http://elf8848.iteye.com/blog/1621392springboot
本身也試了一下,發現子類若是覆蓋父類的方法,確實不能繼承被覆蓋方法的註解。app
可是試了一下spring的註解,即使該註解沒有被繼承到子類上,子類一樣能享有這個註解帶來的效果,這可能和spring的註解掃描和bean加載機制有關,有時間看看源碼吧,這裏先記一下。dom
如下是實驗時寫的代碼:maven
父類:spring-boot
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.PostConstruct; import java.util.UUID; @RestController @ConditionalOnMissingBean(name = "extDemoController") public class DemoController { @PostConstruct public void init(){ System.out.println("---------------DemoController init---------------"); } @Autowired protected OneBean oneBean; @RequestMapping("/random") public String random(){ return UUID.randomUUID().toString(); } }
子類:測試
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.UUID; @RestController public class ExtDemoController extends DemoController { public void init(){ System.out.println("---------------ExtDemoController init---------------"); } public String random(){ System.out.println(oneBean.hello()); return "random from ExtDemoController"; } }
測試代碼1:spa
package com.example.demo; import org.springframework.web.bind.annotation.RequestMapping; import java.lang.reflect.Method; public class CustomTest { public static void main(String[] args) throws NoSuchMethodException { Class<ExtDemoController> clazz = ExtDemoController.class; Method method = clazz.getMethod("random",new Class[]{}); if(method.isAnnotationPresent(RequestMapping.class)){ RequestMapping oneAnnotation = method.getAnnotation(RequestMapping.class); System.out.println(oneAnnotation.name()); } } }
測試代碼2:
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; @SpringBootApplication //@EnableConfigurationProperties(FirstConfig.class) public class DemoApplication { public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(DemoApplication.class, args); for(String name:ctx.getBeanDefinitionNames()){ System.out.println(name); } } }
測試說明:
這是一個普通的springboot項目,maven裏只引入了
spring-boot-starter-web
項目能夠用 http://start.spring.io/ 生成。
測試步驟:
執行測試代碼1,發現註解不能被子類繼承。
執行測試代碼2,發現子類雖未繼承父類內部的三種註解,可是卻徹底能夠享有它們的功能。即訪問 /random 執行的是子類內的邏輯。