SpringBoot中常用注解

1、@PropertySource—加载指定路径的配置文件
@PropertySource只支持properties 文件 不支持 yaml文件。
@PropertySource使用并不影响@ConfigurationProperties

@Component
@ConfigurationProperties(prefix = "person")
@PropertySource(value = "classpath:person.properties")
public class Person {
    private String name;
    private Integer age;
    private Date birthday;
    private Boolean boy;


    private List<Object> list;
    private Map<String,Object> maps;

    private Animal animal;

2、@SpringBootApplication

/**
 *@SpirngBootApplication----标注一个主程序类,说明这是一个SpringBoot应用
 */
@SpringBootApplication
public class HelloMainApplication {
    public static void main(String[] args) {
        //启动Spring应用
        SpringApplication.run(HelloMainApplication.class,args);

    }
}

3、@ImportResource----容器中导入组件
将外部的配置文件加载到程序中来,比如我们定义一个beans.xml文件,里面配置了一个bean,默认情况下这个bean是不会加载到Spring容器中来的。我们需要@ImportResource注解将这个配置文件加载进来。

beans.xml

<?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.xsd">
    <bean id="helloService" class="com.mine.service.HelloService"></bean>
</beans>

package com.mine.service;

public class HelloService {
    
}

SpringBoot里没有Spring配置文件,我们自己编写的配置文件也不能自动识别,想让Spring配置文件生效,加载进来,需要将@ImportResource标注在一个配置类上
@ImportResource(locations="classpath:beans.xml")
@SpringBootApplication
public class HelloMainApplication {
    public static void main(String[] args) {
        //启动Spring应用
        SpringApplication.run(HelloMainApplication.class,args);

    }
}

```java
测试
@RunWith(SpringRunner.class) //使用Spring的驱动器运行
@SpringBootTest
public class ReadPropertiesTest {

    @Autowired
    private Person person;

    @Autowired
    private ApplicationContext ioc;

    @Test
    public void  serviceTest()
    {
        Boolean boo=ioc.containsBean("helloService");
        System.out.println(boo);//true
    }

4、@Bean
SpringBoot 给容器中添加组件推荐使用注解方式 @Configuration @Bean

/**
 * @Configuration---指明当前类是一个配置类
 * 取代配置文件中<bean></bean>这种方式添加组件
 */
@Configuration
public class MyConfig {

    //将方法的返回值添加到容器中,容器中这个组件的默认id就是方法名
    @Bean
    public HelloService helloService()
    {
        return new HelloService();
    }
}

5、@RestController
@RestController注解相当于@ResponseBody + @Controller合在一起的作用

如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,或者html,配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。

如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。

@RestController
public class TestController {

	@RequestMapping("/index")
	public String index() {
		return "user/hello";
	}
}
页面显示的是user/hello字符串
@Controller
public class TestController {

	@RequestMapping("/index")
	public String index() {
		return "user/hello";
	}
}
显示user底下的hello页面上的内容 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值