如何在@meta property属性中给属性附初始值

26402人阅读
spring(4)
引用properties还是要在xml里配置,有两种配置方法,对引用单个properties文件,和多个properties文件
一、读取单个 properties 文件
在 spring 的配置文件中,加入
引入until命名空间:
xmlns:util=&http://www.springframework.org/schema/util&
xsi:schemaLocation=&http://www.springframework.org/schema/util
& & & & http://www.springframework.org/schema/util/spring-util-3.0.xsd&
读取properties文件
&util:properties id=&propertiesReader& location=&classpath:test.properties& /&&
二、读取多个 properties 文件
&bean id=&propertiesReader&
& class=&org.springframework.beans.factory.config.PropertiesFactoryBean&&
& &property name=&locations&&
& & &value&classpath:param.properties&/value&
& & &value&classpath:base.properties&/value&
& &&/list&
& &/property&
两种方法其实都是一样的
在类中读取properties文件属性
在类中需要注入的属性实现 setter 和 getter 方法。
在 setter 方法前,添加 @Value 注解
@Value(&#{propertiesReader[propertiesName]}&)
propertiesName 为 properties 文件中的键。这样,在容器启动过程中, Spring 将自动注入值。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:159795次
积分:1538
积分:1538
排名:千里之外
原创:21篇
转载:30篇
评论:18条
(1)(1)(3)(1)(1)(1)(1)(1)(1)(1)(1)(1)(2)(2)(1)(4)(1)(3)(4)(2)(3)(5)(2)(7)(1)property属性_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
property属性
上传于||暂无简介
阅读已结束,如果下载本文需要使用1下载券
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩5页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。今天看啥 热点:
iOS开发中由属性(property)引发的坑
copy修饰的NSMutableArray属性(property)初始化问题
对于属性:
@property (nonatomic, copy) NSMutableArray *someA
若初始化时使用self.someArray:
self.someArray = [[NSMutableArray alloc] initWithCapacity:200];
[self.someArray addObject:name];
APP Crash,其中关键 Error Info如下:
-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x7f9c89701c20
原因是,通过copy修饰的property,若通过self.someArray =来赋值初始化,则是通过系统合成setter方法实现,由于设置copy修饰词,则返回实际上是不可变数组(NSArray),当调用addObject 方法会报错。
初始化 或者 赋值 部分,做如下修改:
_someArray = [[NSMutableArray alloc] initWithCapacity:200];
则APP运行正常,原因是:_someArray是实例变量,实例变量并没有 copy 修饰,指向的仍是定义的 NSMutableArray 类型。所以即使后面通过 self.someArray 使用 addObject 方法仍然可行,因为初始化赋值阶段获取的是NSMutableArray类型对象
知道最佳解决方案么? 其实,就是把copy修饰词改为strong,因为可变数组对象是个容器,只要其元素中没有和self对象存在相互持有造成内存泄漏的,则不会出现任何问题。
@synthesize @dynamic 正确使用
在之前的文章 Objective-C 2.0 基础要点归纳 中,对属性(property)进行了分析,但没有对关键字 @property 和 @synthesize 以及@dynamic的作用进行对比解析,这部分将对之进行详细分析,但问题却是因 atomic 修饰词引出。
关键字的作用说明
@property 预编译命令(关键字)作用是声明属性的 getter和setter方法 @synthesize 生成属性访问器即 getter和setter方法 @dynamic 禁止编译器生成 getter/setter,通过自定义或者在运行时动态创建绑定:主要使用在Core Data中
atomic 是操作原子性,作为属性修饰词,则编译器生产的 getter/setter方法中存在 @synchronized(self)或者lock锁机制只允许原子性访问。
若只自定义一个属性访问器(setter/getter中一个),会出现warning:writable atomic property &name& cannot pair a synthesized getter with &
若两个都定义,则需要 @synthesize name = _ 两个都自定义的情况下,系统不会合成属性访问器,则此时需要显示调用 @synthesize来定义属性的实例变量名,以便使用属性的实例变量
实践代码:
TestAtomic 类
TestAtomic.h
@interface TestAtomic : NSObject
@property (atomic, copy) NSString *
TestAutomic.m
#import TestAtomic.h
@implementation TestAtomic
@synthesize name = _
- (NSString *)name
if (![_name isEqualToString:@]) {
- (void)setName:(NSString *)name
@synchronized(self) {
if (![_name isEqualToString:name]) {
TestB 类,继承TestAtomic类
#import TestAtomic.h
@interface TestB : TestAtomic
@property (atomic, copy) NSString *B
- (instancetype)initWithSuperN
#import TestB.h
@implementation TestB
- (instancetype)initWithSuperName
self = [super init];
if (self) {
[self setName:@this is A];
- (void)print
NSLog(@%@ --- %@, self.Bname, self.name);
Main 函数:
#include TestAtomic.h
#include TestB.h
int main(int argc, const char * argv[])
@autoreleasepool {
TestB *testB = [TestB new];
testB.name = @
//testB = [testB initWithSuperName];
testB.Bname = @this is B;
[testB print];
NSMutableArray问题
error: writable atomic property cannot pair a synthesized setter/getter
iphone 开发中属性 property 和 synthesize 权威的介绍
iOS中atomic的实现解析
@synthesize和@dynamic区别
相关搜索:
相关阅读:
相关频道:
IOS教程最近更新C# 6.0的属性(Property)的语法与初始值
昨晚有学点新知识,是有关C# 6.0的。
在创建有一张表:
CREATE TABLE [dbo].[ToolLocation]
[ToolLocation_nbr] SMALLINT IDENTITY(1,1) NOT NULL PRIMARY KEY,
[LocationName] NVARCHAR(20) NOT NULL,
[Description] NVARCHAR(50) NULL,
[IsActive] BIT NOT NULL DEFAULT(1)
Source Code
看看前后对比与写法:
using System.Collections.G
using System.L
using System.T
using System.Threading.T
namespace Insus.NET.Models
public class ToolLocation
public short ToolLocation_nbr { } = 1;
public string LocationName { } = string.E
public string Description { } = string.E
public bool IsActive { } =
Source Code
下面Insus.NET演示一下,创建一个实体:
using Insus.NET.M
using System.Collections.G
using System.L
using System.T
using System.Threading.T
namespace Insus.NET.Entities
public class ToolLocationEntity
public IEnumerable ToolLocations()
return new List() {
new ToolLocation(),
new ToolLocation { ToolLocation_nbr = 2, LocationName = &A2&, Description = &A2 CNC&,IsActive = true},
new ToolLocation { ToolLocation_nbr = 3, LocationName = &C4&, Description = &C4 CNC&,IsActive = false}
Source Code
它将会有三个对象,第一个对象是使用默认值。
在控制器中:
在ASP.NET MVC视图中,显示这些数据:
看看运行的效果:
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'

我要回帖

更多关于 s property属性 的文章

 

随机推荐