在Spring中創(chuàng)建Bean有多種方式,以下是常用的幾種方式:
@Component
public class MyBean {
// ...
}
<bean id="myBean" class="com.example.MyBean">
<!-- 設(shè)置屬性值 -->
<property name="name" value="John" />
<!-- 設(shè)置引用類(lèi)型的屬性值 -->
<property name="dependency" ref="myDependency" />
</bean>
@Configuration
public class AppConfig {
@Bean
public MyBean myBean() {
return new MyBean();
}
}
<context:component-scan base-package="com.example" />
以上是幾種常見(jiàn)的創(chuàng)建Bean的方式,根據(jù)具體情況選擇適合的方式來(lái)創(chuàng)建Bean。