温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring的bean管理(注解方式)

发布时间:2020-08-10 22:01:20 来源:网络 阅读:11247 作者:叫我北北 栏目:开发技术
1.Spring的Bean管理的中常用的注解
@Component:组件.(作用在类上)
public interface UserDao {     public void sayHello(); }   @Component(value="userDao")     public class UserDaoImpl implements UserDao {   @Override public void sayHello() {     System.out.println("Hello Spring Annotation..."); }

Spring中提供@Component的三个衍生注解:(功能目前来讲是一致的)

* @Controller :WEB

* @Service :业务层

* @Repository :持久层

 

这三个注解是为了让标注类本身的用途清晰,Spring在后续版本会对其增强

spring的bean管理(注解方式) 

2.属性注入的注解:(使用注解注入的方式,可以不用提供set方法.)

@Value  :用于注入普通类型.

@Autowired :自动装配:

* 默认按类型进行装配.

* 按名称注入:

* @Qualifier:强制使用名称注入.

@Resource相当于:

* @Autowired和@Qualifier一起使用.

spring的bean管理(注解方式)

3.Bean的作用范围的注解:

@Scope:

* singleton:单例

* prototype:多例

4.Bean的生命周期的配置:

@PostConstruct :相当于init-method

@PreDestroy  :相当于destroy-method

    @PostConstruct说明

         @PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Sercletinti()方法。@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。

    @PreConstruct说明

         @PreConstruct修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servletdestroy()方法。@PreConstruct修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前。

 

05.SpringBean管理的方式的比较:

spring的bean管理(注解方式) 

 

XML和注解:

* XML :结构清晰.

* 注解 :开发方便.(属性注入.)

 

实际开发中还有一种XML和注解整合开发:

* BeanXML配置.但是使用的属性使用注解注入


关于注解1:工作中有一次犯了一个很严重的问题:

spring的bean管理(注解方式)

关于注解2:想要两个类使用同一个变量,而且两个类有关系,通过注入方式建立两个类的对象产生关系。但是如果想要共用一个对象,建立对象可以通过有参构造传入(new A("xxx")),但是注解建立对象我不会传参。但可以通过配置文件和注解相结合使用。

spring的bean管理(注解方式)

第二张图演示了properties文件属性的使用方法,在工作中,又遇见另外一种(在这里演示)

首先,在spring的主配文件中要配置:

<!-- 加载applicationConfig.properties文件,获取属性文件的内容 -->	<bean id="propertyConfigurer" class="com.ad.utils.GlobalProperties">	<property name="ignoreResourceNotFound" value="true" />	<property name="locations">	<list>	<value>classpath:applicationConfig.properties</value>	......	</list>	</property>	</bean>

然后写出实现类:

import java.util.HashMap; import java.util.Map; import java.util.Properties; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; /**  * 自定义PropertyPlaceholderConfigurer返回properties内容  *   */ public class GlobalProperties extends PropertyPlaceholderConfigurer{	private static Map<String, Object> ctxPropertiesMap;	@Override	protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,Properties props) throws BeansException {	super.processProperties(beanFactoryToProcess, props);	ctxPropertiesMap = new HashMap<String, Object>();	for (Object key : props.keySet()) {	String keyStr = key.toString();	String value = props.getProperty(keyStr);	ctxPropertiesMap.put(keyStr, value);	}	}	public static Object getContextProperty(String name) {	return ctxPropertiesMap.get(name);	}	/**	 * 获取属性值	 * @param key	 * @return	 */	public static String getProperties(String key){ 	Object value = ctxPropertiesMap.get(key);	return value != null ? String.valueOf(value) : "";	}	 	/**	 *获取属性值,返回×××	 * @param key	 * @return	 */	public static Integer getInteger(String key){	Object value = ctxPropertiesMap.get(key);	return value != null ? Integer.valueOf(value.toString()) : 0;	} }

最后是使用方法:

GlobalProperties.getProperties("XXXXX").trim();


补:spring的bean管理xml方式

01.注入对象类型属性

    创建service类和dao

    service中得到dao对象

spring的bean管理(注解方式)




向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI