网站建设水平,网站建设确认函,即墨有做网站的吗,微信第三方做网站需要费用吗Spring针对Bean管理中创建对象提供的注解有哪些#xff1f;Component#xff1a;普通Service#xff1a;业务逻辑层Controller#xff1a;controller层Repository#xff1a;dao层用注解的方式是为什么#xff1f;简化xml方式开发#xff0c;只需要注解就可以完成在配置…Spring针对Bean管理中创建对象提供的注解有哪些Component普通Service业务逻辑层Controllercontroller层Repositorydao层用注解的方式是为什么简化xml方式开发只需要注解就可以完成在配置文件中的配置如何实现注解开发创建对象组件扫描package com.atguigu.spring5.ComponentScan;import org.springframework.stereotype.Component;/*** BelongsProject: 02-Spring* BelongsPackage: com.atguigu.spring5* Author: dengLiMei* CreateTime: 2023-02-04 16:56* Description: TODO* Version: 1.0*/
//value可以不写如果不写默认是类名的首字母小写
Component(value user)
public class User {public void add() {System.out.println(aaaa);}
}package com.atguigu.spring5.ComponentScan;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** BelongsProject: 02-Spring* BelongsPackage: com.atguigu.spring5.ComponentScan* Author: dengLiMei* CreateTime: 2023-02-10 15:12* Description: TODO* Version: 1.0*/
public class Main {public static void main(String[] args) {ApplicationContext content new ClassPathXmlApplicationContext(beans.xml);User user content.getBean(user, User.class);user.add();}
}?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd!-- 开启组件扫描如果扫描多个包多个包使用逗号隔开扫描包上层目录--!-- 表示 扫描包中的所有类--context:component-scan base-packagecom.atguigu.spring5/context:component-scan!-- use-default-filters“false”表示现在不使用默认filter自己配置filtercontextinclude-filter设置扫描哪些内容--!-- 表示 只扫描注解带Controller的类--context:component-scan base-packagecom.atguigu.spring5 use-default-filtersfalsecontext:include-filter typeannotation expressionorg.springframework.stereotype.Controller//context:component-scan!-- 下面配置扫描包所有内容contextinclude-filter设置扫描哪些内容--!-- 表示 除了Controller其余类都扫描--context:component-scan base-packagecom.atguigu.spring5context:exclude-filter typeannotation expressionorg.springframework.stereotype.Controller/-filtertype expression//context:component-scan
/beans 属性注入Autowired根据类型注入Qualifier根据名称进行注入和AutowiredResource可以根据类型或名称注入Value普通属性注入javaxjava扩展包完全注解开发Configuration作为配置类替代xml配置文件package com.atguigu.spring5.SpringConfiguration;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;Configuration
ComponentScan(basePackages {com.atguigu.spring5})
public class SpringConfigure {}package com.atguigu.spring5.ScopeTest;import org.springframework.stereotype.Component;/*** BelongsProject: 02-Spring* BelongsPackage: com.atguigu.spring5.Book* Author: dengLiMei* CreateTime: 2023-02-08 19:28* Description: TODO* Version: 1.0*/
Component
public class Book {private String bname;private String bauthor;public void setBname(String bname) {this.bname bname;}public void setBauthor(String bauthor) {this.bauthor bauthor;}public static void main(String[] args) {Book book new Book();book.setBname(abc);}public void testDemo() {System.out.println(bname --- aaa);}}package com.atguigu.spring5.SpringConfiguration;import com.atguigu.spring5.ScopeTest.Book;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** BelongsProject: 02-Spring* BelongsPackage: com.atguigu.spring5.SpringConfiguration* Author: dengLiMei* CreateTime: 2023-02-11 09:38* Description: TODO* Version: 1.0*/
public class Main {public static void main(String[] args) {
// 加载配置类ApplicationContext context new AnnotationConfigApplicationContext(SpringConfigure.class);
// 创建实例Book book context.getBean(book, Book.class);
// 调用方法book.testDemo();}
}输出结果 Spring系列文章Spring——是什么作用内容用到的设计模式Spring——Bean管理-xml方式进行属性注入Spring——Bean管理-注解方式进行属性注入Spring——什么是IOCSpring——AOP是什么?如何使用Spring——什么是事务传播行为事务隔离级别有哪些Spring——整合junit4、junit5使用方法如果有想要交流的内容欢迎在评论区进行留言如果这篇文档受到了您的喜欢那就留下你点赞收藏脚印支持一下博主~