package com.example.boot3.Config;
//import com.alibaba.druid.FastsqlException;
import com.example.boot3.bean.User;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Scope;
@SpringBootConfiguration//这是一个SpringBoot配置类
//@Configuration//这是一个配置类
//@Import(FastsqlException.class)//导入任意类作为组件,但是组件的名字默认是全类名!!!!!
public class AppConfig
{
// 组件默认是单实例的
@Scope("prototype")
@Bean//替代Bean标签
public User user(){//默认的Bean名字就是方法名字
var user = new User();
user.setName("Jack");
user.setId(1L);
return user;
}
// 导入第三方Bean
// @Bean
// public FastsqlException fastsqlException(){
// return new FastsqlException();
// }
}
package com.example.boot3.bean;
public class Cat
{
private Long id;
private String name;
public Cat() {
}
public Cat(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Cat{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
package com.example.boot3.bean;
public class Dog {
private Long id;
private String name;
public Dog() {
}
public Dog(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Dog{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
package com.example.boot3.bean;
public class User
{
private Long id;
private String name;
public User() {
}
public User(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>boot3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot3</name>
<description>boot3</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>druid</artifactId>-->
<!-- <version>1.2.16</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.example.boot3.Config;
import com.example.boot3.bean.Cat;
import com.example.boot3.bean.Dog;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
@SpringBootConfiguration
public class AppConfig2 {
@Bean
@ConditionalOnClass(name="com.alibaba.druid.FastsqlException")
public Cat cat01(){
return new Cat();
}
@Bean
@ConditionalOnMissingClass(value="com.alibaba.druid.FastsqlException")
public Dog dog01(){
return new Dog();
}
}
package com.example.boot3;
import com.example.boot3.bean.Cat;
import com.example.boot3.bean.Dog;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan("com.example")//改变包扫描路径
@SpringBootApplication(scanBasePackages = "com.example.boot3")//手动指定扫描包位置
public class Boot3Application
{
public static void main(String[] args) {
var ioc = SpringApplication.run(Boot3Application.class, args);
// for (String name : ioc.getBeanDefinitionNames()) {
// System.out.println(name);
// }
// Object user = ioc.getBean("user");
// Object user1 = ioc.getBean("user");
// System.out.println(user == user1);
// System.out.println(ioc.getBean("fastsqlException"));
for (String s : ioc.getBeanNamesForType(Cat.class)) {
System.out.println(s);
}
for (String s : ioc.getBeanNamesForType(Dog.class)) {
System.out.println(s);
}
}
}
<?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="user" class="com.example.boot3.bean.User" scope="prototype">
<property name="id" value="1"></property>
<property name="name" value="Jack"></property>
</bean>
<bean id="cat" class="com.example.boot3.bean.Cat">
<property name="id" value="2"></property>
<property name="name" value="Rose"></property>
</bean>
</beans>
标签:name,框架,Spring,Boot,public,import,com,id,String
From: https://blog.51cto.com/u_16322355/9233020