从厦门回来的2024/4/9 晚上23点的bean注解
Bean注册
如果要注册的bean对象来自第三方(不是自定义的),是无法使用@Component及衍生注解声明bean的
@Bean
安装jar包到maven本地仓库脚本
mvn install:install-file -Dfile=C:\Users\Administrator\Desktop\资料\02_Bean注册资料\common-pojo-1.0-SNAPSHOT.jar -DgroupId=cn.itcast -DartifactId=common-pojo -Dversion=1.0 -Dpackaging=jar
package com.atguigu.boot3web;
import cn.itcast.pojo.Country;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class Boot3WebApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(Boot3WebApplication.class, args);
Country country = context.getBean(Country.class);
System.out.println(country);
}
//注入country对象
@Bean
public Country country(){
return new Country();
}
}
控制台输出
2024-04-09 23:08:05.055 INFO 16668 --- [ main] c.atguigu.boot3web.Boot3WebApplication : Starting Boot3WebApplication using Java 17.0.9 on LAPTOP-F1PA7UHF with PID 16668 (D:\work\shanguigu\boot3-web\target\classes started by 114514 in D:\work\shanguigu\boot3-web)
2024-04-09 23:08:05.057 INFO 16668 --- [ main] c.atguigu.boot3web.Boot3WebApplication : No active profile set, falling back to 1 default profile: "default"
2024-04-09 23:08:05.719 INFO 16668 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2024-04-09 23:08:05.728 INFO 16668 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2024-04-09 23:08:05.728 INFO 16668 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.68]
2024-04-09 23:08:05.813 INFO 16668 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2024-04-09 23:08:05.813 INFO 16668 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 722 ms
2024-04-09 23:08:06.010 INFO 16668 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [static/index.html]
2024-04-09 23:08:06.081 INFO 16668 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2024-04-09 23:08:06.088 INFO 16668 --- [ main] c.atguigu.boot3web.Boot3WebApplication : Started Boot3WebApplication in 1.318 seconds (JVM running for 1.954)
Country{name='null', system='null'}
Process finished with exit code 130
然后呢,放在启动类是不建议的


浙公网安备 33010602011771号