导航:首页 > 文件管理 > spring注解的配置文件

spring注解的配置文件

发布时间:2024-07-05 05:45:55

1. spring的常用注解是什么

Spring Boot常用注解

1、@SpringBootApplication

替代 @SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan

2、@ImportAutoConfiguration

导入配置类,一般做测试的时候使用,正常优先使用@EnableAutoConfiguration

3、@SpringBootConfiguration

替代@Configuration

4、@ImportResource

将资源导入容器

5、@PropertySource

导入properties文件

6、PropertySources

@PropertySource 的集合

(1)spring注解的配置文件扩展阅读:

Controller 该类为Controller。

RequestMapping 配置方法路径等信息。

ResponseBody 返回值,例如JSON,XML。

PathVariable 获取RESTFUL路径中的值如 /company/{corpId}/dept/{deptId}。

RequestParam 获取Request参数值如xxx?from=index_nav。

Component,Repository,Service。

一般用Repository,service用Service,需要多个service时,一般用Componet。

2. spring怎么配置注解

@Repository注解:

1 package imooc_spring.test.anotation.myrepository;

2

3 import org.springframework.stereotype.Repository;

4

5 /**

6 * 指定id,默认为dAO,即类名首字母小写,如果指定了名称那么只能ctx.getBean(指定名称)来获取bean

7 * 这个例子里就只能通过ctx.getBean("wyl)来获取DAO 的实例了;

8 *

9 * @author Wei

10 */

11 @Repository("wyl")

12 public class DAO {

13 /**

14 * 返回x和y的乘积

15 *

16 * @param x

17 * @param y

18 * @return x*y

19 */

20 public int multi(int x, int y) {

21 return x * y;

22 }

23 }

复制代码

@Component 注解:

复制代码

1 package imooc_spring.test.anotation;

2

3 import org.springframework.stereotype.Component;

4 /**

5 * Component 注解

6 * @author Wei

7 *

8 */

9 @Component

10 public class TestObj {

11 public void SayHi(){

12 System.out.println(" Hi this is TestObj.SayHi()...");

13 }

14 }

复制代码

@Controller注解闷吵:

复制代码

1 package imooc_spring.test.anotation;

2

3 import org.springframework.stereotype.Controller;

4

5 @Controller

6 public class UserController {

7 public void execute(){

8 System.out.println(" UserController.execute()...");

9 }

10 }

复制代码

@Repository注解:

复制代码

1 package imooc_spring.test.anotation;

2

3 import org.springframework.stereotype.Repository;

4

5 //@Repository

6 @Repository("wyl_repo")

7 public class UserRepositoryImpl implements IUserRepository {

8 //模誉拍拟持久化层

9 庆罩羡 @Override

10 public void save() {

11 // TODO Auto-generated method stub

12 System.out.println(" UserRepositoryImpl.save()...");

13 }

14

15 }

复制代码

@Service注解:

复制代码

1 package imooc_spring.test.anotation;

2

3 import org.springframework.stereotype.Service;

4

5 @Service

6 public class UserService {

7 public void add(){

8 System.out.println(" UserService.add()...");

9 }

10 }

3. SpringBoot项目使用@PropertySource注解加载properties配置文件,但输出对象值为null

不是要加上前缀?
@ConfigurationProperties(prefix = "Example")
@PropertySource("classpath:my.properties")//这是属性文件路径

4. java开发中spring注解的使用方法

注解方式李态配置(简化XML配置文件)
组件自动扫描功能
首先需要在applicationContext.xml中添加<context:component-scan/>
a.扫描Bean组件的注解,替代xml中的<bean>元素的定义。
@Service 用于Service业务组件
@Control 用于Action控制组件
@Respository 用于DAO数据访问组哪源源件
@Component 用于其他组件
Bean组件扫描到容器后,裂羡默认名字为类名(首字母小写)
如果需要自定义名称可以使用@Service("id名")
b.依赖注入的注解标记
@Resource 按名称@Resource(name="id名")
@AutoWired 按名称
@Autowired
@Qualifier("id名")
c.其他注解
@Scope 等价于<bean scope="">
@PostConstruct 等价于<bean init-method="">
@PreDestroy 等价于<bean destroy-method="">

阅读全文

与spring注解的配置文件相关的资料

热点内容
摩拜汽车用的哪个app 浏览:781
手机能开机不进系统文件系统 浏览:312
windows7软激活工具 浏览:955
两个jsp页面怎么传值 浏览:307
在编程时如何思考问题并解决 浏览:3
现在学的是什么编程 浏览:781
用go语言编程需要下载什么 浏览:637
vb语言如何确定数据类型 浏览:108
怎么查找微云里的文件在哪里找 浏览:450
群文件容量满了怎么办 浏览:727
深度u盘系统工具包 浏览:297
linux查看单独文件的属性 浏览:495
那个版本的显卡驱动有1080 浏览:57
iphone4s如何关闭网络 浏览:854
特斯拉app如何寻找充电桩 浏览:491
单个文件最大支持多少 浏览:685
什么网站租房覆盖到县城 浏览:914
怎么设置电脑网络共享给手机 浏览:449
kindle安卓pdf文件 浏览:323
linux复制光盘文件 浏览:550

友情链接