您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
SpringBoot2.0中@Configuration注解有什么用,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
通过配置文件beans.xml配置user和dog的bean
<?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="user01" class="com.dashi.bean.User"> <property name="name" value="zhangsan"/> <property name="age" value="18"/> </bean> <bean id="dog01" class="com.dashi.bean.Pet"> <property name="name" value="哈利"/> </bean> </beans>
创建ApplicationContext获取user bean
/**
* spring方式通过getbean
/*
public static void main(String[] args) {
ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:beans.xml");
User user01 = (User) ac.getBean("user01");
}现在我们通过原始的spring的方式去管理这两个bean**接下来我们通过spring boot的@Configuration注解来实现bean的管理
创建配置类MyConfig(该名称可以自定义),该类通过@Configuration注解
通过@Configuration注解的类就相当于spring的beans.xml文件,通过@Bean注解的方法即为一个个的bean。bean中的属性即为spring中的property属性
/**
* 相当于spring中的bean.xml的<bean id="user01"></bean>
*/
@Configuration
public class Myconfig {
//方法的名称就是spring bean中的bean id 该方法为”user01“,属性为property
@Bean
public User user01(){
return new User("zhangsan",18);
}
//方法的名称就是spring bean中的bean id 该方法为”dogPet“ 属性为property
@Bean
public Pet dogPet(){
return new Pet("tom");
}
}得到user和Pet的实体类
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
//SpringApplication.run(MainApplication.class);
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class);
// String[] names = run.getBeanDefinitionNames();
// for(String name:names){
// System.out.println(name);
// }
User user01 = run.getBean("user01", User.class);
System.out.println(user01.getName());
Pet dogPet = run.getBean("dogPet", Pet.class);
System.out.println(dogPet.getName());
}
}运行结果如下:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.6.RELEASE) 2021-05-09 10:45:08.692 INFO 15880 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2021-05-09 10:45:08.692 INFO 15880 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2021-05-09 10:45:09.136 INFO 15880 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2021-05-09 10:45:09.758 INFO 15880 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8888 (http) with context path '' 2021-05-09 10:45:09.786 INFO 15880 --- [ main] com.dashi.MainApplication : Started MainApplication in 4.501 seconds (JVM running for 7.13) zhangsan tom
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。