spring和springboot
来源:互联网 发布:c语言void和int 编辑:程序博客网 时间:2024/06/05 15:27
spring常用到的注解:
@Service用于标注业务层组件
@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问组件,即DAO组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Autowired后不需要getter()和setter()方法,Spring也会自动注入。
在接口前面标上@Autowired注释使得接口可以被容器注入,如:
- @Autowired
- @Qualifier("chinese")
- private Man man;
当接口存在两个实现类的时候必须使用@Qualifier指定注入哪个实现类,否则可以省略,只写@Autowired。
使用@Autowired时你的OrganDaoIbatis 必须以@Service或@Component注解才行。
之前用户使用的是3个注解注解他们的main类。分别是@Configuration,@EnableAutoConfiguration,@ComponentScan。由于这些注解一般都是一起使用,spring boot提供了一个统一的注解@SpringBootApplication。
@SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan。
- @SpringBootApplication
- public class ApplicationMain {
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
- }
同等于:
- @Configuration
- @EnableAutoConfiguration
- @ComponentScan
- public class ApplicationMain {
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
- }
分开解释@Configuration,@EnableAutoConfiguration,@ComponentScan。
1、@Configuration:提到@Configuration就要提到他的搭档@Bean。使用这两个注解就可以创建一个简单的spring配置类,可以用来替代相应的xml配置文件。
派生到我的代码片
- <beans>
- <bean id = "car" class="com.test.Car">
- <property name="wheel" ref = "wheel"></property>
- </bean>
- <bean id = "wheel" class="com.test.Wheel"></bean>
- </beans>
相当于:
派生到我的代码片
- @Configuration
- public class Conf {
- @Bean
- public Car car() {
- Car car = new Car();
- car.setWheel(wheel());
- return car;
- }
- @Bean
- public Wheel wheel() {
- return new Wheel();
- }
- }
@Configuration的注解类标识这个类可以使用Spring IoC容器作为bean定义的来源。@Bean注解告诉Spring,一个带有@Bean的注解方法将返回一个对象,该对象应该被注册为在Spring应用程序上下文中的bean。
2、@EnableAutoConfiguration:能够自动配置spring的上下文,试图猜测和配置你想要的bean类,通常会自动根据你的类路径和你的bean定义自动配置。
3、@ComponentScan:会自动扫描指定包下的全部标有@Component的类,并注册成bean,当然包括@Component下的子注解@Service,@Repository,@Controller。
- @ResponseBody
用该注解修饰的函数,会将结果直接填充到HTTP的响应体中,一般用于构建RESTful的api;
- @Controller
用于定义控制器类,在spring 项目中由控制器负责将用户发来的URL请求转发到对应的服务接口(service层)。
- @RestController
@ResponseBody和@Controller的合集
- @RequestMapping
提供路由信息,负责URL到Controller中的具体函数的映射。
- @EnableAutoConfiguration
Spring Boot自动配置(auto-configuration):尝试根据你添加的jar依赖自动配置你的Spring应用。例如,如果你的classpath下存在HSQLDB,并且你没有手动配置任何数据库连接beans,那么我们将自动配置一个内存型(in-memory)数据库”。你可以将@EnableAutoConfiguration或者@SpringBootApplication注解添加到一个@Configuration类上来选择自动配置。如果发现应用了你不想要的特定自动配置类,你可以使用@EnableAutoConfiguration注解的排除属性来禁用它们。例子代码如下:
- @ComponentScan
表示将该类自动发现(扫描)并注册为Bean,可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。
- @Configuration
相当于传统的xml配置文件,如果有些第三方库需要用到xml文件,建议仍然通过@Configuration类作为项目的配置主类——可以使用@ImportResource注解加载xml配置文件。
- @SpringBootApplication
相当于@EnableAutoConfiguration、@ComponentScan和@Configuration的合集。
- @Import
用来导入其他配置类。
- @ImportResource
用来加载xml配置文件。
- @Autowired
自动导入依赖的bean
- @Service
一般用于修饰service层的组件
- @Repository
使用@Repository注解可以确保DAO或者repositories提供异常转译,这个注解修饰的DAO或者repositories类会被ComponetScan发现并配置,同时也不需要为它们提供XML配置项
使用mybatis注解需要的配置。如下面的代码所示,使用@MapperScan来扫描注册mybatis数据库接口类,其中basePackages属性表明接口类所在的包,sqlSessionTemplateRef表明接口类使用的SqlSessionTemplate。如果项目需要配置两个数据库,@MapperScan也需要分别配置。
阅读全文
0 0
- spring和springboot
- Junit和Spring、SpringBoot测试
- SpringBoot:spring boot 服务配置和部署
- SpringBoot整合Spring Security和Mybatis验证
- spring和springboot 多数据源配置和问题解决
- springboot学习三(springboot结合spring data jpa和freemarker显示)
- SpringBoot入门系列:Spring Security 和 Angular JS(1)
- SpringBoot入门系列:Spring Security 和 Angular JS(2)
- 关于spring和springboot +mybatis事物管理的问题!
- SpringBoot:spring boot使用Druid和监控配置
- SpringBoot: spring boot使用Druid和监控配置
- spring升级版springBoot
- SpringBoot - Spring Security学习
- 【Spring Boot】springboot + swagger
- springboot+mybatis+spring security
- SpringBoot~Spring Security入门
- 定时器--Spring--SpringBoot
- springboot整合spring-security
- python 编码问题 UnicodeDecodeError: ‘utf8’ codec can’t decode byte 0xb4 in position 0: invalid start byt
- HTML CSS 属性
- 万维链
- bigchaindb源码分析(九)——选举
- ie兼容性问题(持续更新)
- spring和springboot
- K
- 程序员如何增加收入?
- bigchaindb源码分析(十)——总结
- IE8异步上传附件
- Hadoop+zookeeper+hive高可用集群部署文档
- Maven项目的搭建过程+jetty
- Android 调试工具介绍
- 2015-2016 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2015) Adjoin the Networks (树的直径)